Configuration

Blair Conrad edited this page Jan 24, 2016 · 7 revisions

Include the NuGet package. (Choose the one for the platform you want, the PCL dependency library is automatically added.)

Request the logger when you need it:

public sealed partial class LogSamplePage : Win8Sample.Common.LayoutAwarePage
{
    private ILogger Log = LogManagerFactory.DefaultLogManager.GetLogger<LogSamplePage>();

Log information when you need to:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        
        // flat strings...
        if (this.Log.IsInfoEnabled)
            this.Log.Info("I've been navigated to.");

        // formatting...
        if (this.Log.IsDebugEnabled)
            this.Log.Debug("I can also format {0}.", "strings");

        // errors...
    		try
    		{
    			this.DoMagic();
    		}
    		catch(Exception ex)
    		{
                if (this.Log.IsWarnEnabled)
    				this.Log.Warn("You can also pass in exceptions.", ex);
    		}
    }

For Windows Store apps, you might want to configure the WindowsStoreAppCrashHandler. You may also want to use the XamlExtensionMethods.

Adding your own targets

If you want to choose a target configuration outside of the DefaultConfiguration, change the value of DefaultConfiguration before you load the first target.

    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;

        // change the config...
#if DEBUG
        LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new FileStreamingTarget());
        LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new SQLiteTarget());
#else
        LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Info, LogLevel.Fatal, new FileStreamingTarget());
        LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Info, LogLevel.Fatal, new SQLiteTarget());
#endif

        // setup the global crash handler...
        GlobalCrashHandler.Configure();
    }