Friday, September 23, 2016

Log4Net - steps

Thought I would jot down some notes for log4Net. There is not really much to log4net other than it is a logging framework for .NET  (For Java, it is log4j or the core to slf4j).

3 components need  to be created to get it to work with .NET 4.5+.

1.       Use NuGet to install the log4net package to your projects
2.       Include log4net in the assembly and base configurations.
3.       Create an logger instance per class to be logged and call using log levels.

After install the package using NuGet.
I find the core AssemblyInfo.cs file

Add :
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

In the Web.config or app.config (depending on your application startup configuration)
You declare the namespace in the <configSections /> element

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

And then create the log4net element with root and Appender definitions (see below).  The Root element is where you define logging level that you want and can be specific to the appender.  --For production and QA environments, you will want to avoid “ALL, DEBUG” unless you mean to use this for specific cases.

Logging Levels:
1.       OFF
2.       FATAL
3.       ERROR
4.       WARN
5.       INFO
6.       DEBUG
7.       ALL

Filters can be applied here as well as the type of logging “appenders.” For MySystem, I defined a ConsoleAppender for the debugging sessions and a FileAppender in order grab logging events while the application is running in environments that we cannot debug. There are other types of appenders, but these are the most widely used (see the Apache website). This gives a PatternLayout logging of
“Data LogLevel ClassLogger -  Message logged with new line and exception message (if Found)”

2016-09-16 14:40:56,461 INFO MySystem.Areas.Jobs.Controllers.PostTicketsController - inside PostTicketsController index try else send email id :1201
2016-09-16 14:41:17,677 ERROR MySystem.Models.EmailModels - System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.0.0.27:25
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)…

  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="MyFileAppender" />
    </root>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline%exception" />
      </layout>
    </appender>
    <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="App_Data\mysystem.log" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline%exception" />
      </layout>
    </appender>
  </log4net>

In the class that I want to have create a logger, I use the following code:

using log4net;
private static ILog log = LogManager.GetLogger(typeof(Startup));// this is using typeof(thisclass)
<!—- OR -->
private static readonly ILog logger = LogManager.GetLogger (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//generic logger for any class
       
//Since it is part of the assembly, you may not have to declare the using but declare the logger like
private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(Startup));


Then simply, in the line of code that I want to log.
            log.Info("MySystem Server app starting up ...");
            log.Debug("Why are you looking at my stuff ... object.property is " + object.property);
            log.Error("Exception occurred " + ex);

Deployment notes:
For a project, the main Server project and the MySystem.Core project had to be updated for logging to work. Reminder, if the root log level is higher than the class logging, the logging does not get performed.  In the above examples, if the logging level is ERROR or WARN, the Info and Debug will not be logged.

Some caveats,
Earlier editions of log4net required you to specify the .NET Framework target and is supposedly corrected in current editions.
First caution with logging is that too much logging can cause performance issues.
Also be careful about logging SQL messages directly into a log (especially a database-orientated using the AdoNetAppender log) as that can be a path for SQL-injection and becomes another system dependency at runtime.
A RollingFileAppender, FileAppender requires write access to the directory.

More Info (including more appenders and .NET framework specific notes):
http://logging.apache.org/log4net/
http://www.codeproject.com/Articles/140911/log-net-Tutorial


“The journey of a thousand miles begins with a single step.” (Lao Tzu)


No comments:

Post a Comment