Open sandboxFocusImprove this doc

Logging with NLog

This article shows how to use PostSharp Logging and NLog together.

Writing PostSharp Logging events to NLog

To send PostSharp Logging event to NLog targets:

  1. Add PostSharp logging to your codebase as described in Getting Started with PostSharp Logging.

  2. Add the PostSharp.Patterns.Diagnostics.NLog package to your startup project.

  3. In the application startup file, include the following namespace imports:

    using NLog;
    using NLog.Config;
    using NLog.Targets;
    using PostSharp.Patterns.Diagnostics;
    using PostSharp.Patterns.Diagnostics.Backends.NLog;
    

    In the application startup method, include the following code:

    // Configure NLog.
    var nlogConfig = new LoggingConfiguration();
    
    var fileTarget = new FileTarget("file")
    {
        FileName = "nlog.log",
        KeepFileOpen = true,
        ConcurrentWrites = false,
    };
    
    nlogConfig.AddTarget(fileTarget);
    nlogConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, fileTarget));
    
    var consoleTarget = new ConsoleTarget("console");
    nlogConfig.AddTarget(consoleTarget);
    nlogConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));
    
    LogManager.EnableLogging();
    
    
    // Configure PostSharp Logging to use NLog.
    LoggingServices.DefaultBackend = new NLogLoggingBackend(new LogFactory(nlogConfig));
    

    This example code instructs NLog to write all log records to a file named nlog.log and to the console. If you prefer, you can configure NLog with a configuration file. See the NLog documentation for details.

If you run your application, you should now see a detailed log in a file named nlog.log. If your application is a console application, you should also see the log in the console.

Collecting NLog events into PostSharp Logging

If your source code already emits log events using NLog, you can configure PostSharp Logging to collect log events directly emitted by the NLog API, so that they are all processed by the same PostSharp backend and NLog logger. One of the benefits is that indentation of manual logging and automatic logging will be synchronized. See Collecting Logs from Other Frameworks for more information.

Suppose that you have already configured NLog and PostSharp like this:

// Configure NLog
var configuration = new XmlLoggingConfiguration("nlog.config");

LogManager.Configuration = configuration; // Set it as the default configuration
LogManager.EnableLogging();

// Configure PostSharp Logging to use NLog
LoggingServices.DefaultBackend = new NLogLoggingBackend(new LogFactory(configuration));

// Emit manual log records. Note that this logger will skip PostSharp Logging, so indentation
// will not be respected.
NLog.Logger logger = LogManager.GetCurrentClassLogger();
logger.Info().Message( "Hello, {color} sky!", "blue" ).Write();

To collect NLog manual logging events into PostSharp, replace that startup code with the following:

// Set up NLog to send all logging events to the PostSharp collector:
var target = new NLogCollectingTarget("toPostSharp"); // the name does not matter
var sourceConfiguration = new LoggingConfiguration();
sourceConfiguration.AddTarget( target ); 
sourceConfiguration.LoggingRules.Add( new LoggingRule("*", global::NLog.LogLevel.Trace, target) ); // Capture all events.
LogManager.Configuration = sourceConfiguration; // Set it as the default configuration
LogManager.EnableLogging();

// Create a separate NLog configuration that contains the targets for your final output. 
// If you have it in a configuration file, you can load it with:
var outputConfiguration = new XmlLoggingConfiguration("nlog.config");

// Use this final configuration to create a PostSharp backend and set it as the default backend:
LoggingServices.DefaultBackend = new NLogLoggingBackend(new LogFactory(outputConfiguration));

You can now do standard NLog logging and the logging output will be collected and sent to PostSharp Logging:

NLog.Logger logger = LogManager.GetCurrentClassLogger();
logger.Info().Message( "Hello, {color} sky!", "blue" ).Write();

See Also

Reference

NLogLoggingBackend
NLogCollectingTarget
Other Resources

Example project: PostSharp.Samples.Logging.NLog
Collecting Logs from Other Frameworks