Log4Net Agent

Use the Log4Net Agent to post events to Loggr from a .NET desktop app or ASP.NET web app.

Before You Get Started
Installing From NuGet
Installing From Github
Posting Events
Handling Exceptions
Troubleshooting

Before You Get Started

Requirements

  • Microsoft .NET 2.0 Framework or greater is required.
  • Full version of the .Net Framework is required, client profiles will not work.

Other Notes

Make sure log4net is using the web.config for its configuration, or adapt the settings accordingly

This agent builds on top of the .NET/C# Agent which means you can follow along here to log using your existing investment in Log4Net and also tap into some of the more advanced logging scenarios using the .NET/C# Agent.

This agent also supports by VB.NET (and other .NET languages). We just use C# for the example code in this document.

top

Installing From NuGet

The preferred method of installation is using a NuGet package. If you don't use NuGet or you need to modify the source code of this agent, see Installing From Github.

Note

If you are new to NuGet or need to install it, see Installing Nuget from the Nuget.org website.


The first step is to install the package to your .NET app.

Visual Studio Package Manager
NuGet Package Manager If you use the Visual Studio Package Manager, as shown to the right, open the Package Manager by choosing Tools > Library Package Manager > Manage Nuget Packages...

From the left-hand accordian, choose Online > All.This will show you all packages available online. In the search box at the top-right, enter "loggr". You should see 2 packages. Select the package named loggr-log4net and click the Install button. This will install the package for you.

Package Manager Console
If you install NuGet packages from the Package Manager Console, open the console and enter the following at the PM prompt.

PM> Install-Package loggr-log4net

Edit web.config or app.config
To configure you'll need to add your LOGKEY and APIKEY to your app's config file. NuGet will have added a couple lines to your config file, find the section in your config file that looks like:

  <loggr>
    <log>
      <!-- Get your logkey and apikey from your Loggr log settings-->
      <add key="logKey" value="__ADD_YOUR_LOGKEY_HERE__"/>
      <add key="apiKey" value="__ADD_YOUR_APIKEY_HERE__"/>
    </log>
  </loggr>

Replace the appropriately labeled values with your LOGKEY and APIKEY.

Next, configure Log4Net to use the Log4Net Appender:

    <configSections>
      <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
    </configSections>

    ...

    <log4net>
      <appender name="LoggrAppender" type="Loggr.Log4Net.Appender,loggr-log4net"></appender>
      <root>
        <level value="ALL" />
        <appender-ref ref="LoggrAppender" />
      </root>    
    </log4net>

Jump to Posting Events to continue.

top

Installing From Github

If you do not use NuGet or would like to tweak the agent for your own purposes, you can find pre-compiled binaries and source code on Loggr's Github page.

Add assembly to bin folder
Whether you grab the latest assembly from the bin folder on Github or you compile the source yourself, you'll need to copy the loggr-dotnet.dll assembly and the loggr-log4net.dll into your app's bin folder. After the file is copied, edit your app's config file.

Edit web.config or app.config
To finish up you'll need to add several lines to your app's config file. If you're building a web app, open web.config in a text editor. If you're building a desktop app, open app.config in a text editor (you may need to add this file if your project doesn't currently have one).

Add the following to the appropriate section of your config file:
<configuration>

  ...

  <configSections>
    <sectionGroup name="loggr">
      <section name="log" type="System.Configuration.NameValueSectionHandler"></section>
    </sectionGroup>
  </configSections>

  ...

  <loggr>
    <log>
      <add key="logKey" value="__ADD_YOUR_LOGKEY_HERE__"></add>
      <add key="apiKey" value="__ADD_YOUR_APIKEY_HERE__"></add>
    </log>
  </loggr>

  ...

  <log4net>
      <appender name="LoggrAppender" type="Loggr.Log4Net.Appender,loggr-log4net"></appender>
      <root>
        <level value="ALL" />
        <appender-ref ref="LoggrAppender" />
      </root>    
  </log4net>

  ...

</configuration>

Replace the appropriately labeled values with your LOGKEY and APIKEY. Jump to Posting Events to continue.

top

Posting Events

When properly configured you shouldn't have to change your Log4Net logging code. When your app generates some log entries, just view your log in Loggr and you should see your entries.

In some cases you'll want more control over your events and the data they contain. In this case, you can bypass the Log4Net interface and directly use the .NET/C# Agent interface as detailed here.

top

Handling Exceptions

Log4Net doesn't have any facility for specifically logging .NET exceptions, but again, you can leverage Loggr's .NET/C# Agent for that.

Using Loggr to track application exceptions is very helpful. It's even better when using the .NET/C# agent because it automatically formats the exception event for you. All you have to do is trap an exception using a Try/Catch or within a global error handler and then post the exception to Loggr.

To post a .NET exception to Loggr use:

try 
{
    ## some code that generates an exception
} 
catch (Exception ex) 
{
    Loggr.Events.CreateFromException(ex).Post()
}

If you need to add any information to the event before posting, use the built-in methods:

Loggr.Events.CreateFromException(ex)
    .Text("This was an error: $$")
    .Source("myapp")
    .AddTags("critical")
    .Post()

You can easily log unhandled exceptions in an ASP.NET app. Edit the Global.asax.cs file. Add the following code to Application_Error event:

protected void Application_Error(object sender, EventArgs e)
{
    // get last exception
    Exception ExHttp = ((HttpApplication)sender).Server.GetLastError();

    // supress 404's
    if (ExHttp.GetType() == typeof(HttpException))
    {
        if (((HttpException)ExHttp).GetHttpCode() == 404)
            return;
    }

    // these are unhandled exceptions, so use the inner if available
    if (ExHttp.InnerException != null)
        ExHttp = ExHttp.InnerException;

    // post exception to Loggr
    Loggr.Events.CreateFromException(ExHttp).Post();
}

top

Troubleshooting

Using the .NET agent is pretty simple. Here are some things to help you figure out why it's not working.

You're not able to add the agent assembly as a reference to your app:
  • Make sure you're using at least version 2.0 of the .NET Framework.
  • Make sure you are not using the .NET Client Profile (use the full .NET Framework).
If you're posting events and they are not showing up in your event log, check the following:
  • Make sure your machine is connected to the internet.
  • Make sure the correct LOGKEY and APIKEY are configured in web.config (or app.config)?
  • Make sure you're looking at the correct day in the Loggr website.
  • Check your event log for any posting errors.

top