Skip to content

Instantly share code, notes, and snippets.

@rstackhouse
Created August 22, 2011 13:22
Show Gist options
  • Select an option

  • Save rstackhouse/1162357 to your computer and use it in GitHub Desktop.

Select an option

Save rstackhouse/1162357 to your computer and use it in GitHub Desktop.

Revisions

  1. rstackhouse revised this gist Aug 22, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -55,7 +55,7 @@ namespace ConsoleApplication1

    config.LoggingRules.Add(dbRule);

    LogManager.Configuration = config;
    LogManager.Configuration = config; //doesn't work if moved after instatiation of config

    _logger = LogManager.GetCurrentClassLogger();

  2. rstackhouse created this gist Aug 22, 2011.
    65 changes: 65 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NLog;
    using NLog.Common;
    using NLog.Targets;
    using NLog.Config;

    namespace ConsoleApplication1
    {
    class Program
    {
    private static Logger _logger;

    static void Main(string[] args)
    {
    //InternalLogger.LogToConsole = true;
    //InternalLogger.LogLevel = LogLevel.Trace;

    var config = new LoggingConfiguration();

    var target =
    new FileTarget{
    FileName=typeof(Program).FullName + ".log"
    };

    config.AddTarget("logfile", target);

    var dbTarget = new DatabaseTarget();

    dbTarget.ConnectionString = @"<server>;Initial Catalog=<database>;Persist Security Info=True;User ID=<user>;Password=<password>";

    dbTarget.CommandText =
    @"INSERT INTO [Log] (Date, Thread, Level, Logger, Message, Exception)
    VALUES(GETDATE(), @thread, @level, @logger, @message, @exception)";

    dbTarget.Parameters.Add(new DatabaseParameterInfo("@thread", new NLog.Layouts.SimpleLayout("${threadid}")));

    dbTarget.Parameters.Add(new DatabaseParameterInfo("@level", new NLog.Layouts.SimpleLayout("${level}")));

    dbTarget.Parameters.Add(new DatabaseParameterInfo("@logger", new NLog.Layouts.SimpleLayout("${logger}")));

    dbTarget.Parameters.Add(new DatabaseParameterInfo("@message", new NLog.Layouts.SimpleLayout("${message}")));

    dbTarget.Parameters.Add(new DatabaseParameterInfo("@exception", new NLog.Layouts.SimpleLayout("${exception}")));

    config.AddTarget("database", dbTarget);

    var rule = new LoggingRule("*", LogLevel.Debug, target);

    config.LoggingRules.Add(rule);

    var dbRule = new LoggingRule("*", LogLevel.Debug, dbTarget);

    config.LoggingRules.Add(dbRule);

    LogManager.Configuration = config;

    _logger = LogManager.GetCurrentClassLogger();

    _logger.Debug("Using programmatic config");
    }
    }
    }