NXLog Docs

.NET application logs

NXLog can be used to capture logs directly from Microsoft .NET™ applications using third-party utilities. This guide demonstrates how to set up these utilities with a sample .NET application and a corresponding NXLog configuration.

This guide uses the SharpDevelop IDE, but Microsoft Visual Studio™ on Windows, or MonoDevelop on Linux could also be used. The log4net package and log4net.Ext.Json extension are also required.

The following instructions were tested with SharpDevelop 5.1.0, .NET 4.5, log4net 2.0.5, and log4net.Ext.Json 1.2.15.14586. To use NuGet packages without the NuGet package manager, simply download the nupkg file using the "Download" link, add a .zip extension to the file name, and extract.
  1. Create a new Solution in SharpDevelop by selecting File  New  Solution and choosing the Console Application option. Enter a name and click Create.

  2. Place the log4net and log4net.Ext.Json DLL files in the bin\Debug directory of your project.

  3. Select Project  Add Reference. Open the .NET Assembly Browser tab and click Browse. Add the two DLL files so that they appear in the Selected References list, then click OK.

    Add Reference dialog in SharpDevelop
  4. Edit the AssemblyInfo.cs file (under Properties in the Projects sidebar) and add the following line.

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "App.config", Watch = true)]
    Editing the AssemblyInfo.cs file
  5. Click the Refresh icon in the Projects sidebar to show all project files.

  6. Create a file named App.config in the bin\Debug folder, open it for editing, and add the following code. Update the remoteAddress value of the with the IP address (or host name) of the NXLog instance.

    App.config
    <configuration>
      <configSections>
        <section name="log4net"
                 type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      </configSections>
    
      <log4net>
        <appender name="UdpAppender" type="log4net.Appender.UdpAppender">
          <remoteAddress value="192.168.56.103" />
          <remotePort value="514" />
          <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json" />
        </appender>
    
        <root>
          <level value="DEBUG"/>
          <appender-ref ref="UdpAppender"/>
        </root>
      </log4net>
    </configuration>
  7. Edit the Program.cs file, and replace its contents with the following code. This loads the log4net module and creates some sample log messages.

    Program.cs
    using System;
    using log4net;
    
    namespace demo
    {
        class Program
        {
            private static readonly log4net.ILog mylog = log4net.LogManager.GetLogger(typeof(Program));
            public static void Main(string[] args)
            {
                log4net.Config.BasicConfigurator.Configure();
                mylog.Debug("This is a debug message");
                mylog.Warn("This is a warn message");
                mylog.Error("This is an error message");
                mylog.Fatal("This is a fatal message");
                Console.ReadLine();
            }
        }
    }
  8. Configure NXLog.

    nxlog.conf
    <Extension _json>
        Module  xm_json
    </Extension>
    
    <Input in>
        Module  im_udp
        Host    0.0.0.0
        Port    514
        <Exec>
            $raw_event =~ s/\s+$//;
    
            # Parse JSON into fields for later processing if required
            parse_json();
        </Exec>
    </Input>
    
    <Output out>
        Module  om_file
        File    "/tmp/output"
    </Output>
    
    <Route r>
        Path    in => out
    </Route>
  9. In SharpDevelop, press the F5 key to build and run the application. The following output should appear.

    Demo output
    4301 [1] DEBUG demo.Program (null) - This is a debug message
    4424 [1] WARN demo.Program (null) - This is a warn message
    4425 [1] ERROR demo.Program (null) - This is an error message
    4426 [1] FATAL demo.Program (null) - This is a fatal message
  10. Examine the /tmp/output file. It should show the sample log entries produced by the .NET application.

    NXLog output
    {"date":"2014-03-19T09:41:08.7231787+01:00","Level":"DEBUG","AppDomain":"demo.exe","Logger":"demo.Program","Thread":"1","Message":"This is a debug message","Exception":""}
    {"date":"2014-03-19T09:41:08.8456254+01:00","Level":"WARN","AppDomain":"demo.exe","Logger":"demo.Program","Thread":"1","Message":"This is a warn message","Exception":""}
    {"date":"2014-03-19T09:41:08.8466327+01:00","Level":"ERROR","AppDomain":"demo.exe","Logger":"demo.Program","Thread":"1","Message":"This is an error message","Exception":""}
    {"date":"2014-03-19T09:41:08.8476223+01:00","Level":"FATAL","AppDomain":"demo.exe","Logger":"demo.Program","Thread":"1","Message":"This is a fatal message","Exception":""}
Disclaimer

While we endeavor to keep the information in this topic up to date and correct, NXLog makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability, or availability of the content represented here. We update our screenshots and instructions on a best-effort basis.

Last revision: 17 September 2018