Skip to content

Instantly share code, notes, and snippets.

@rstackhouse
Created August 22, 2011 13:22
Show Gist options
  • Save rstackhouse/1162357 to your computer and use it in GitHub Desktop.
Save rstackhouse/1162357 to your computer and use it in GitHub Desktop.
Configure NLog Programmatically
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; //doesn't work if moved after instatiation of config
_logger = LogManager.GetCurrentClassLogger();
_logger.Debug("Using programmatic config");
}
}
}
@GrantByrne
Copy link

This is pretty awesome. I just copied an pasted this and it immediately wrote the log to a text file.

What am I missing to actually output this to a sql server 2008 database?

@angeldimitrov94
Copy link

Excellent! This helped me quickly figure out how to progammatically add a DB target to my NLogger.

ヘ( ^o^)ノ\(^_^ )

@UweKeim
Copy link

UweKeim commented Dec 9, 2022

There is a newer method XmlLoggingConfiguration.CreateFromXmlString that allows to have an in-memory string with an XML and let NLog automatically create the configuration from that string, e.g.:

LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString("...");

@Mecanik
Copy link

Mecanik commented Jan 10, 2023

There is a newer method XmlLoggingConfiguration.CreateFromXmlString that allows to have an in-memory string with an XML and let NLog automatically create the configuration from that string, e.g.:

LogManager.Configuration = XmlLoggingConfiguration.CreateFromXmlString("...");

Thanks for pointing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment