Log4Net into my projects

Log4Net is the most powerful login engine I have come across.
Logging is essential in each application>
A framework like Log4Net allows you to log to any place you want to:
File, database, etc….
In this blog I’m going to explain how to implement this engine in a simple Command Prompt project.

  1. Step 1 create a simple Command Prompt project in Visual Studio
    2. Install the package withe Nugget, just search for ‘log4net’
    3. Add a file to our project called: log4net.config
    4. Next to make this framework working we need to tell our application were to find it’s configuration, open the AssemblyInfo.cs file of your project and add the following line:
 
 [assembly: log4net.Config. XmlConfigurator(ConfigFile = "log4net.config")] 
 

Which point to our config file we just created.

  1. The Log4Net uses the concept off apenders which you could see as ‘handlers‘, this will become clear in a minute.
    5.  I’m going to use “ColoredConsoleAppenderClass
    https://logging.apache.org/log4net/release/sdk/html/T_log4net_Appender_ColoredConsoleAppender.html
 
   <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
 
  1. Each appender in the framework has mappings we need to define in our config.
    These mapping are linked to the kind of log level you are going to use:
    Warning, Error, Fatal, Info
    Let add our mappings into out config:
 <mapping>

      <level value="WARN"/>

      <foreColor value="Yellow"/>

    </mapping>

    <mapping>

      <level value="ERROR"/>

      <foreColor value="Red"/>

    </mapping>

    <mapping>

      <level value="FATAL"/>

      <foreColor value="White"/>

      <backColor value="Red" />

    </mapping>

    <mapping>

      <level value="INFO"/>

      <foreColor value="Cyan"/>

    </mapping>

    <mapping>

      <level value="DEBUG"/>

      <foreColor value="Green"/>

    </mapping>

  
  1. Next we need to determine how our log lines will look like
    In our config there is a section layout:
    <layout type="log4net.Layout.PatternLayout">

      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>

    </layout>
 
  1. Let’s wrap it up our complete config.xml:

<log4net>

  <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">

    <mapping>

      <level value="WARN"/>

      <foreColor value="Yellow"/>

    </mapping>

    <mapping>

      <level value="ERROR"/>

      <foreColor value="Red"/>

    </mapping>

    <mapping>

      <level value="FATAL"/>

      <foreColor value="White"/>

      <backColor value="Red" />

    </mapping>

    <mapping>

      <level value="INFO"/>

      <foreColor value="Cyan"/>

    </mapping>

    <mapping>

      <level value="DEBUG"/>

      <foreColor value="Green"/>

    </mapping>

    <layout type="log4net.Layout.PatternLayout">

      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>

    </layout>

  </appender>

  <root>

    <level value="DEBUG" />

    <appender-ref ref="ColoredConsoleAppender"/>

  </root>

</log4net>

 

9.Let’s see how our code look like:


using log4net;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;

namespace SBS.Blog.Log4Net
 {
 class Program
 {
 private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

static void Main(string[] args)
 {

Logger.Debug("This is a Debug statement");
 Logger.Info("This is a Info statement");
 Logger.Warn("This is a Warn statement");
 Logger.Error("This is a Error statement");
 Logger.Fatal("This is a Fatal statement");
 Console.ReadLine();
 }
 }
 }

  1. This will result in the following output:

2018-06-11 20_56_52-C__Users_Sebastian_source_repos_SBS.Blog.Log4Net_SBS.Blog.Log4Net_bin_Debug_SBS.

  1. If you want to use the EventLogAppender you could use the following configuration into our log4net.config
 <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >

       <applicationName value="MyApp" />

       <layout type="log4net.Layout.PatternLayout">

             <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

       </layout>

</appender>

 

  1. If you want to write to a file you can use the following configuration

<appender name="FileAppender" type="log4net.Appender.FileAppender">

    <file value="${TMP}\log-file.txt" />

    <appendToFile value="true" />

    <lockingModel type="log4net.Appender.FileAppender+InterProcessLock" />

    <layout type="log4net.Layout.PatternLayout">

        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

    </layout>

</appender>
 

The result of the FileAppender:

loggedtofile

 

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s