Using Log4Net for Application Insights

Getting started with Application Insights (AI) in a new or existing application is very easy. From Visual Studio 2013 Update 3 it is even integrated right into the New Web Project dialog:

 

image

 

When you select this option, Visual Studio will automatically create a new instrumentation key for you that identifies the web application, and insert the necessary Javascript into your master layout page that takes care of sending usage information to Application Insights. Try running your application, click around a few times, and you will see information showing up in the Azure portal within, literally, a few seconds.

NB: you can also perform this operation later on, by select XXXX from the context menu in Solution Explorer.

This will give you a lot of information such as page views, response times, user information like browser version and geographic location. If you want to add custom tracing, you can do this using the Application Insights Telemtry SDK It exists both for .NET code and JavaScript, so you can add tracing both on the backend and the frontend.

Now, this is very nice for a new project but what if you have an existing application that already contains a lot of code for writing trace and debug information? Perhaps you are using Log4Net or the TraceListener class to emit diagnostic information in some way. Well, the good news is that the nice fellows over at Microsoft thought about this. They have created a set of public NuGet packages that makes it very easy to forward the information that you are logging to Application Insights. This means that you won’t have to rewrite any of the existing code and still have the tracing information show up in the portal.

For example, if you are using Log4Net you can use the Microsoft.ApplicationInsights.Log4NetAppender package that will send the information you log using the Log4Net API to AI. It is implemented as a standard Log4Net Appender class, which makes it very easy to use. Just add the package to the projects that performs logging, and the following information will be added to your configuration file:

<log4net> <root> <level value="ALL"/> <appender-ref ref="aiAppender"/> </root> <appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message%newline"/> </layout> </appender> </log4net>

Now, you existing logging Log4Net code..

log4net.Config.XmlConfigurator.Configure(); var logger = log4net.LogManager.GetLogger(this.GetType()); logger.Info("Some information message"); logger.Warn("A warning message"); logger.Error("An error message");

   

Will end up as Trace information in the Azure Portal;

 

image

 
Since this is a regular log4net appender, you can apply the standard filters, to include or exclude certain types of information.

Microsoft has also implemented NuGet packages for NLog and also a TraceListener class, for use when you do standard .NET tracing. In addition, there are some 3rd party packages that covers other logging frameworks such as Serilog:

 

image

 

Happy logging!

10 thoughts on “Using Log4Net for Application Insights”

  1. Originally posted on: http://geekswithblogs.net/jakob/archive/2014/11/09/using-log4net-for-application-insights.aspx#644171

    <p>I’ve been trying to redirect my log4net to Application Insights but just don’t seem to be getting the trace through in much the same way as others with the same problem.</p>

    <p>So I’ve tried to create a canonical example using an ASP.NET MVC template in VS2013 Update 4. Nothing special, just ticked the box to wire in Application Insights, added NuGet package for the Log4Net appender then added the configuration for the appender to web.config and code to the default controller action both taken from above.</p>

    <p>I see my requests appearing in the Application Insights portal now – one request and another for the Page View which was logged from the layout page. There is a distinct lack of any Trace information :(</p>

    <p>Do you have any hints as to why the trace isn’t getting through?</p>

  2. Originally posted on: http://geekswithblogs.net/jakob/archive/2014/11/09/using-log4net-for-application-insights.aspx#646709

    I just installed Microsoft.ApplicationInsights.Log4NetAppender on my windows console project so I can log log4net logs to ApplicationInsights. I cannot find an instruction on how I can specify the instrumentation key that I want to use with ApplicationInsights. Does anyone know how?

    I looked at http://jan-v.nl/post/using-application-insights-in-your-log4net-application but it does not seem to help.

    I also tried the following code to set the instrumentation key via code, but it does not seem to help either.

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    static void Main(string[] args)
    {
    var appenders = log.Logger.Repository.GetAppenders();

    foreach(var appender in appenders)
    {
    if(appender.Name == “aiAppender”)
    {
    ApplicationInsightsAppender appInsightAppender = (ApplicationInsightsAppender)appender;
    appInsightAppender.InstrumentationKey = “xxxxxx”;

    }
    }

    log.Error(“logging something “);
    }
    }

  3. In the case of web projects, to be able to specify your Instrumentation Key you should also install the Microsoft.ApplicationInsights.Web package from Nuget and it will create a ApplicationInsights.config file.

    Within this config file, you add xxx at the top (under

Leave a Reply

Your email address will not be published.