working with log4net package

7.3k views Asked by At

I recently added the log4net package in to my WCF web application. now I have few questions I:

  1. I added the log4net package through the VS 2013 package install and it added whole package. My guess is that log4net is just the dll and I can add it to my project just by adding the dll?

  2. when I added log4net package a packages.config file has been added to my project with this content:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="log4net" version="2.0.3" targetFramework="net45" />
    </packages>
    

what is it? can I remove it?

  1. Can I use the log4net all over my project without defining it in top of each class?

  2. I want to add a extra field to message part. for example I want to log the events like this:

    Log.Debug(IP,"This is a DEBUG level message. Typically your most VERBOSE level.");

and the log file show it like this:

72 DEBUG 2015-06-16 08:17:41,713 [10] [(null)] [InsertDate] - from source IP(192.168.1.1) This is a DEBUG level message.  Typically your most VERBOSE level.
  1. I also added %line in to conversionPattern but it doesn't work. every time it start from 72. How can this be fixed?

  2. How can log the application stop and start in the wcf application?

1

There are 1 answers

1
Yeldar Kurmangaliyev On BEST ANSWER

You actually asked 4 questions in 1. First, you need to understand that you have used a NuGet package manager and it is a good practice to include available packages using NuGet but not manually as it automatically places package in a proper place, lets you add this package easily to other projects of solution, provides versioning etc. Read more about NuGet. For example, here.

Now about your questions:

First: No, log4net package contains not only a log4net.dll file. It also has log4net.xml file. Yes, you can add these two files manually without using NuGet package manager.

Second: It is a list of NuGet packages in your project. You can only remove it if you are not going to use NuGet. However, it is not a good idea - let it be there, it is a single light-weight XML-file.

Third: It's a question of OOP. Create a singleton global logger, for example:

public static class LoggingFactory
{
    private static ILog _logger;

    private static ILog CreateLogger()
    {
        // Some log4net initialization
        return LogManager.GetLogger("Logger");
    }

    public static ILog GetLogger()
    {
        return _logger ?? (_logger = CreateLogger());
    }
}

public class AnyClassOfYourWholeSolution
{
    LoggingFactory.GetLogger().DebugFormat("{0} {1}", a, b);
}

Fourth: It is a question of OOP again. Create an adapter and use it:

public interface ILogger
{
    void Debug(string ip, string message);
    void Info(string ip, string message);
    void Error(string ip, string message);
}

public class Log4NetLogger : ILogger
{
    private ILog  _logger;

    public Log4NetLogger() 
    {
        // Some log4net initialization
        _logger = LogManager.GetLogger("Logger");
    }

    public void Debug(string ip, string message)
    {
        // ...
    }

    public void Info(string ip, string message)
    {
        // ...
    }

    public void Error(string ip, string message)
    {
        // ...
    }
}

public static class LoggingFactory
{
    private static ILogger _loggerAdapter;

    private static Initialize(ILogger adapter)
    {
        _loggerAdapter = adapter;
    }

    public static GetLogger()
    {
        return _logger;
    }
}

public class BeginningOfYourProject 
{
    // Main() or Global.asax or Program.cs etc.
    LoggingFactory.Initialize(new Log4NetLogger());
}

public class AnyClassOfYourWholeProject
{
    LoggingFactory.GetLogger().Debug(ip, message);
}

Or you can extract ip to the properties of log4net:

// During initialization
log4net.ThreadContext.Properties["ip"] = ip;

// log4net Config
<layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%n%-5p %d %t IP: %property{ip} %m" />
</layout>

It is all up to your fantasy.