Support Logging setup Log4Net and .NET logs

Log4Net and .NET Logs

Send and manage logs from .NET using the log4net-loggly package. This package for Log4Net 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 logging alternatives, please see the Advanced Options section.

Log4Net and .NET Log Management Setup

1. Install the log4net-loggly package

Install the log4net-loggly package from NuGet or from Github. To install from the NuGet in the Microsoft Visual Studio, Go to Tools -> NuGet Package Manager -> Package Manager Console as below and run the below command in the console:

Install-Package log4net-loggly

NuGet Package Manager Console 

Then add it as a dependency in the references of your application. Make sure you are using .Net Framework 4 or higher.

2. Log4net Configuration

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

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

AssemblyInfo.cs is located under the Solution Explorer -> Properties in the Microsoft Visual Studio as shown below. Add this line under the Assembly Attribute section:

AssemblyInfo.cs

Paste following into the web.config/app.config of your project so that log4net can send your logs to Loggly, and insert your own customer token below. Make sure the configSections block is the first element of the configuration in app.config. This is a requirement set by .NET.

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>

Add this config that includes the customer token:

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

Replace:

  • TOKEN: your customer token from the source setup page
  • log4net: you could replace tag value with your custom tag name or add additional tags separated by comma.

3. Add Logging To Your App

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 logs.

var logger = LogManager.GetLogger(typeof(<YourClassName>));
 
//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);
  • <YourClassName>: Replace with your class name

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();

4. Try Sending Logs

Run your application to send new logs events to Loggly.

5. Verify Events

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 Example

&nbsp;

Using Log4net Layout

1. Define Log4net conversion pattern

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="https://logs-01.loggly.com/" />
      <inputKey value="TOKEN" />
      <tag value="log4net" />
    </appender>
</log4net>

2. Try Sending Logs

Run your application to send new logs events to Loggly.

3. Verify Events

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 Example

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 (for Input mode) and minimum of 5 seconds (for Bulk mode) 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 secure port 443.

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.