Documentation forLoggly

Log4Net and .NET Logs

Loggly provides the infrastructure to aggregate and normalize log events so they are available to explore interactively, build visualizations, or create threshold-based alerting. In general, any method to send logs from a system or application to an external source can be adapted to send logs to Loggly. The following instructions provide one scenario for sending logs to Loggly.

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 the 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
  • tag: you could add your custom tags separated by commas.

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

Flushing logs on application shutdown
The library is buffering and sending log messages to Loggly asynchronously in the background. This means that some logs may be still in the buffer when the application terminates. To make sure that all logs have been sent you need to cleanly shutdown log4net logger using the following code:

logger.Logger.Repository.Shutdown(); 

This flushes any pending messages.

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

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.
  • HTTP/S Endpoint – You can also program your own library against our HTTP/S endpoint directly.
  • Fields Blacklisting/Excluding – If you want to blacklist/exclude some specific fields from your log events, you can find a very basic, useful project here to see how you can achieve this with one of the Loggly’s supported .NET Serilog library. Please note that the blacklisting implementation is independent of any library so you can easily integrate the blacklisting code even with our most popular log4net-loggly library too.
  • 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 you flush any pending log events before application shutdown. See "Flushing logs on application shutdown" paragraph above.
  • 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.

The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.