Created
August 22, 2011 13:22
-
-
Save rstackhouse/1162357 to your computer and use it in GitHub Desktop.
Configure NLog Programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
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("...");
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
Excellent! This helped me quickly figure out how to progammatically add a DB target to my NLogger.
ヘ( ^o^)ノ\(^_^ )