Log4net extension - add additional data to each log

We are working on logging into our applications using log4net. We would like to receive certain information automatically with each call. The code that calls log.Info or log.Warn should call them normally, without specifying this information.

I am looking for a way to create something that we can connect to log4net. Something between ILog applications is used to register and add, so that we can somehow put this information in a log message. Either in ThreadContext or in LogEventInfo.

The information we are about to capture is related to asp.net; Request URL, user agent, etc. There is also information from the application .config file that we want to include (application identifier).

I want to get between normal ILog.Info and appender so that this information is also automatically included for third-party libraries that also use log4net (Nhibernate, NServiceBus, etc.).

Any suggestions on where to expand will be?

thank

+3
source share
1 answer

What you are looking for is called the log event context. This guide explains how this works:

http://www.beefycode.com/post/Log4Net-Tutorial-pt-6-Log-Event-Context.aspx

In particular, you are interested in the chapter "Estimated Context Values".

Update:

, . , - ID ( ). , URL , :

public class RequestUrlContext
{
    public override string ToString()
    {
        string url;
        // retrieve url from request
        return url;
    }
}

, , , , , . " ", .

+2

All Articles