Log4Net and .NET Logs

Send and manage logs from .NET using the log4net-loggly package. This will send logs directly from your application to Loggly over HTTP/S. It supports plain text or JSON logs. It’s asynchronous and will send logs in the background without blocking your application. For alternatives, please see the Advanced Options section.

Log4Net and .NET Log Management Setup

  1. Install the log4net-loggly package
  2. Install the log4net-loggly package from NuGet or from Github. Then add it as a dependency in the references of your application. Make sure you are using .Net Framework 4 or higher.

    Install-Package log4net-loggly

  3. Log4net Configuration
  4. If you haven’t already, add an attribute in your AssemblyInfo.cs to read the log4net config file

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

    Paste following into the web.config/app.config of your project so that log4net can send to Loggly, and insert your own customer token below.

    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <root>
          <level value="ALL" />
          <appender-ref ref="LogglyAppender" />
        </root>
        <appender name="LogglyAppender" type="log4net.loggly.LogglyAppender, log4net-loggly">
          <rootUrl value="http://logs-01.loggly.com/" />
          <inputKey value="TOKEN" />
          <tag value="log4net" />
        </appender>
    </log4net>
    

    Replace:

  5. Add Logging To Your App
  6. To use this package, first construct a new Logger and then use the log methods to send logs to Loggly. You can send plaintext, exceptions, or JSON.

    var logger = LogManager.GetLogger(typeof(Class));
     
    //Send plaintext
    logger.Info("your log message");
    
    //Send an exception
    logger.Error("your log message", new Exception("your exception message"));
    
    //Send a JSON object
    var items = new Dictionary<string,string>();
    items.Add("key1","value1");
    items.Add("key2", "value2");
    logger.Info(items);
    

    For Console Application:

    You should add the following statement at the end of your Main method as the log4net-loggly library is asynchronous so there needs to be time for the threads the complete logging before the application exits.

    Console.ReadKey();
    

  7. Try Sending Logs
  8. Run your application to send new logs events to Loggly.

  9. Verify Events
  10. Search Loggly for events from your log4net over the past 20 minutes. It may take a few minutes to index the event. If it doesn’t work, see the troubleshooting section below.

    tag:log4net

    Log4Net and .NET Logs

Using Log4net Layout

  1. Define Log4net conversion pattern
  2. You can use log4net Layout conversion patterns to send logs in your own format. See example below.

    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <root>
          <level value="ALL" />
          <appender-ref ref="LogglyAppender" />
        </root>
        <appender name="LogglyAppender" type="log4net.loggly.LogglyAppender, log4net-loggly">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger %message" />
          </layout>
          <rootUrl value="http://logs-01.loggly.com/" />
          <inputKey value="TOKEN" />
          <tag value="log4net" />
        </appender>
    </log4net>
    

  3. Try Sending Logs
  4. Run your application to send new logs events to Loggly.

  5. Verify Events
  6. Search Loggly for events from your log4net over the past 20 minutes. It may take a few minutes to index the event. If it doesn’t work, see the troubleshooting section below.

    tag:log4net
    

    log4net_pattern

Advanced Log4Net and .NET Logging Options

  • Context properties – add GlobalContext and LogicalThreadContext properties.

    You can add the above properties by adding the following tags in your log4net-loggly Appender configuration.

    <logicalThreadContextKeys value="lkey1,lkey2" />
    <globalContextKeys value="gkey1,gkey2" />
    
  • Log4net Layout – Use Layouts in log4net-loggly library to render logs in your desired format.
  • File Monitoring in Windows – Log to file from your .NET program, and then nxlog can read them and send to Loggly.
  • Loggly Libraries Catalog – Find additional libraries for sending to Loggly including log4net
  • HTTP/S Endpoint – You can also program your own library against our HTTP/S endpoint directly.
  • Search or post your own Log4Net or .NET logging questions, examples, or configurations in the community forum.

Log4Net and .NET Log Troubleshooting

If you don’t see any data show up in the verification step, then check for these common problems.

Check log4net-loggly:

    • Wait a few minutes in case indexing needs to catch up
    • This library is asynchronous so make sure it has a second or two to send the logs before the application exits.
    • Make sure the configSections block is the first element of the configuration in app.config. This is a requirement set by .NET.
    • Use a packet capture tool like Wireshark to make sure events are being sent to Loggly successfully. You should see outbound events to logs-01.loggly.com on port 80.

Still Not Working?

  • Search or post your own Apache log4net, using log4net, file appender, or log4net tutorial questions in the community forum.
Thanks for the feedback! We'll use it to improve our support documentation.


Top