public void AutoFlushTargetWrapperSyncTest1() { var myTarget = new MyTarget(); var wrapper = new AutoFlushTargetWrapper { WrappedTarget = myTarget, }; myTarget.Initialize(null); wrapper.Initialize(null); var logEvent = new LogEventInfo(); Exception lastException = null; bool continuationHit = false; AsyncContinuation continuation = ex => { lastException = ex; continuationHit = true; }; wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation)); Assert.True(continuationHit); Assert.Null(lastException); Assert.Equal(1, myTarget.FlushCount); Assert.Equal(1, myTarget.WriteCount); continuationHit = false; wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation)); Assert.True(continuationHit); Assert.Null(lastException); Assert.Equal(2, myTarget.WriteCount); Assert.Equal(2, myTarget.FlushCount); }
public void AutoFlushTargetWrapperAsyncTest1() { var myTarget = new MyAsyncTarget(); var wrapper = new AutoFlushTargetWrapper(myTarget); myTarget.Initialize(null); wrapper.Initialize(null); var logEvent = new LogEventInfo(); Exception lastException = null; var continuationHit = new ManualResetEvent(false); AsyncContinuation continuation = ex => { lastException = ex; continuationHit.Set(); }; wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation)); continuationHit.WaitOne(); Assert.Null(lastException); Assert.Equal(1, myTarget.FlushCount); Assert.Equal(1, myTarget.WriteCount); continuationHit.Reset(); wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation)); continuationHit.WaitOne(); Assert.Null(lastException); Assert.Equal(2, myTarget.WriteCount); Assert.Equal(2, myTarget.FlushCount); }
public void AutoFlushTargetWrapperAsyncTest2() { var myTarget = new MyAsyncTarget(); var wrapper = new AutoFlushTargetWrapper(myTarget); myTarget.Initialize(null); wrapper.Initialize(null); var logEvent = new LogEventInfo(); Exception lastException = null; for (int i = 0; i < 100; ++i) { wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(ex => lastException = ex)); } var continuationHit = new ManualResetEvent(false); AsyncContinuation continuation = ex => { continuationHit.Set(); }; wrapper.Flush(ex => { }); Assert.Null(lastException); wrapper.Flush(continuation); Assert.Null(lastException); continuationHit.WaitOne(); Assert.Null(lastException); wrapper.Flush(ex => { }); // Executed right away Assert.Null(lastException); Assert.Equal(100, myTarget.WriteCount); Assert.Equal(103, myTarget.FlushCount); }
public void MultipleConditionalAutoFlushWrappersTest() { var testTarget = new MyTarget(); var autoFlushOnLevelWrapper = new AutoFlushTargetWrapper(testTarget); autoFlushOnLevelWrapper.Condition = "level > LogLevel.Info"; var autoFlushOnMessageWrapper = new AutoFlushTargetWrapper(autoFlushOnLevelWrapper); autoFlushOnMessageWrapper.Condition = "contains('${message}','FlushThis')"; testTarget.Initialize(null); autoFlushOnLevelWrapper.Initialize(null); autoFlushOnMessageWrapper.Initialize(null); AsyncContinuation continuation = ex => { }; autoFlushOnMessageWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "test").WithContinuation(continuation)); Assert.Equal(1, testTarget.WriteCount); Assert.Equal(0, testTarget.FlushCount); autoFlushOnMessageWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Fatal, "*", "test").WithContinuation(continuation)); Assert.Equal(2, testTarget.WriteCount); Assert.Equal(1, testTarget.FlushCount); autoFlushOnMessageWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "Please FlushThis").WithContinuation(continuation)); Assert.Equal(3, testTarget.WriteCount); Assert.Equal(2, testTarget.FlushCount); }
public void IgnoreExplicitAutoFlushWrapperTest() { var testTarget = new MyTarget(); var bufferingTargetWrapper = new BufferingTargetWrapper(testTarget, 100); var autoFlushOnLevelWrapper = new AutoFlushTargetWrapper(bufferingTargetWrapper); autoFlushOnLevelWrapper.Condition = "level > LogLevel.Info"; autoFlushOnLevelWrapper.FlushOnConditionOnly = true; testTarget.Initialize(null); bufferingTargetWrapper.Initialize(null); autoFlushOnLevelWrapper.Initialize(null); AsyncContinuation continuation = ex => { }; autoFlushOnLevelWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "test").WithContinuation(continuation)); Assert.Equal(0, testTarget.WriteCount); Assert.Equal(0, testTarget.FlushCount); autoFlushOnLevelWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Fatal, "*", "test").WithContinuation(continuation)); Assert.Equal(2, testTarget.WriteCount); Assert.Equal(1, testTarget.FlushCount); autoFlushOnLevelWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "Please do not FlushThis").WithContinuation(continuation)); Assert.Equal(2, testTarget.WriteCount); Assert.Equal(1, testTarget.FlushCount); autoFlushOnLevelWrapper.Flush(continuation); Assert.Equal(2, testTarget.WriteCount); Assert.Equal(1, testTarget.FlushCount); }
static void Main(string[] args) { FileTarget wrappedTarget = new FileTarget(); wrappedTarget.FileName = "${basedir}/file.txt"; AutoFlushTargetWrapper target = new AutoFlushTargetWrapper(); target.WrappedTarget = wrappedTarget; NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); Logger logger = LogManager.GetLogger("Example"); logger.Debug("log message"); }
public static void SetLoggerToNormal() { FileTarget target = new FileTarget(); target.FileName = "${basedir}/nlog/autoFlushLogger.txt"; target.KeepFileOpen = true; target.Encoding = Encoding.UTF8; target.ArchiveOldFileOnStartup = true; AutoFlushTargetWrapper wrapper = new AutoFlushTargetWrapper(); wrapper.WrappedTarget = target; wrapper.OptimizeBufferReuse = true; SimpleConfigurator.ConfigureForTargetLogging(wrapper, LogLevel.Debug); }
public void AutoFlushOnConditionTest() { var testTarget = new MyTarget(); var autoFlushWrapper = new AutoFlushTargetWrapper(testTarget); autoFlushWrapper.Condition = "level > LogLevel.Info"; testTarget.Initialize(null); autoFlushWrapper.Initialize(null); AsyncContinuation continuation = ex => { }; autoFlushWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Info, "*", "test").WithContinuation(continuation)); autoFlushWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "test").WithContinuation(continuation)); Assert.Equal(2, testTarget.WriteCount); Assert.Equal(0, testTarget.FlushCount); autoFlushWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Warn, "*", "test").WithContinuation(continuation)); autoFlushWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Error, "*", "test").WithContinuation(continuation)); Assert.Equal(4, testTarget.WriteCount); Assert.Equal(2, testTarget.FlushCount); }
/// <summary> /// Set up logger table target. /// </summary> /// <remarks> /// This is setup by code because the target is also used by code to recover the content of the logged messages. /// The target is recovered by code using : /// <code>LogManager.Configuration.FindTargetByName<AutoFlushTargetWrapper>("flushedTableTarget");</code> /// </remarks> private static void ExtendLogConfigurationWithTableTarget() { if (LogManager.Configuration == null) { throw new NullReferenceException("LogManager.Configuration must not be null."); } var tableTarget = new TableTarget { Name = "tableTarget" }; var autoFlushTargetWrapper = new AutoFlushTargetWrapper { WrappedTarget = tableTarget, Name = "flushedTableTarget" }; LogManager.Configuration.AddTarget("flushedTableTarget", autoFlushTargetWrapper); var loggingRule = new LoggingRule("*", LogLevel.Trace, autoFlushTargetWrapper); LogManager.Configuration.LoggingRules.Add(loggingRule); LogManager.ReconfigExistingLoggers(); var targetByName = LogManager.Configuration.FindTargetByName <AutoFlushTargetWrapper>("flushedTableTarget"); if (targetByName == null) { throw new NLogRuntimeException("Could not retrieve target 'flushedTableTarget' by name."); } if (targetByName.WrappedTarget == null) { throw new NLogRuntimeException("Target wrapper 'flushedTableTarget' does not contain a wrapped target."); } var retrievedTableTarget = targetByName.WrappedTarget as TableTarget; if (retrievedTableTarget == null) { throw new NLogRuntimeException("Target 'flushedTableTarget' wrapped target is not of type 'TableTarget'."); } }
public void AutoFlushTargetWrapperAsyncWithExceptionTest1() { var myTarget = new MyAsyncTarget { ThrowExceptions = true, }; var wrapper = new AutoFlushTargetWrapper(myTarget); myTarget.Initialize(null); wrapper.Initialize(null); var logEvent = new LogEventInfo(); Exception lastException = null; var continuationHit = new ManualResetEvent(false); AsyncContinuation continuation = ex => { lastException = ex; continuationHit.Set(); }; wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation)); continuationHit.WaitOne(); Assert.NotNull(lastException); Assert.IsType <InvalidOperationException>(lastException); // no flush on exception Assert.Equal(0, myTarget.FlushCount); Assert.Equal(1, myTarget.WriteCount); continuationHit.Reset(); lastException = null; wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation)); continuationHit.WaitOne(); Assert.NotNull(lastException); Assert.IsType <InvalidOperationException>(lastException); Assert.Equal(0, myTarget.FlushCount); Assert.Equal(2, myTarget.WriteCount); }
public void ConfigureLogging() { // Intialize Config Object LoggingConfiguration config = new LoggingConfiguration(); // Initialize Console Target var consoleTarget = new ColoredConsoleTarget("Console Target") { Layout = @"${time} ${longdate} ${uppercase: ${level}} ${logger} ${message} ${exception: format=ToString}" }; // Initialize the AsyncWrapper for the ConsoleTarget AsyncTargetWrapper consoleWrapper = new AsyncTargetWrapper(); consoleWrapper.WrappedTarget = consoleTarget; consoleWrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Block; consoleWrapper.QueueLimit = 5000; // Initialize the AsyncFlushTargetWrapper for the ConsoleWrapper AutoFlushTargetWrapper consoleFlushWrapper = new AutoFlushTargetWrapper(); consoleFlushWrapper.WrappedTarget = consoleWrapper; // This condition controls when the log is flushed. Set the LogLevel to be equivalent to the maximum level specified in the targetRule consoleFlushWrapper.Condition = "level >= LogLevel.Trace"; // Adding the target to the config config.AddTarget("console", consoleFlushWrapper); // Initialize File Target var fileTarget = new FileTarget("File Target") { FileName = "Logs\\${cached:cached=true:Inner=${date:format=dd.MM.yyyy HH-mm}:CacheKey=${shortdate}}.log", KeepFileOpen = false, Layout = @"[${date:format=dd.MM.yyyy HH-mm-ss}] ${message} ${exception: format=ToString}" }; // Initialize the AsyncWrapper for the fileTarget AsyncTargetWrapper fileWrapper = new AsyncTargetWrapper(); fileWrapper.WrappedTarget = fileTarget; fileWrapper.QueueLimit = 5000; fileWrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Block; // Initialize the AsyncFlushTargetWrapper for the FileWrapper AutoFlushTargetWrapper fileFlushWrapper = new AutoFlushTargetWrapper(); fileFlushWrapper.WrappedTarget = fileWrapper; // This condition controls when the log is flushed. Set the LogLevel to be equivalent to the maximum level specified in the targetRule fileFlushWrapper.Condition = "level >= LogLevel.Trace"; // Adding the target to the config config.AddTarget("file", fileFlushWrapper); // Creating the Log Level rules for each target and adding them to the config // Edit these to change what methods are logged var fileRule = new LoggingRule("*", fileTarget); fileRule.EnableLoggingForLevels(LogLevel.Trace, LogLevel.Info); fileRule.EnableLoggingForLevel(LogLevel.Error); config.LoggingRules.Add(fileRule); var consoleRule = new LoggingRule("*", consoleTarget); consoleRule.EnableLoggingForLevels(LogLevel.Trace, LogLevel.Info); consoleRule.EnableLoggingForLevel(LogLevel.Error); config.LoggingRules.Add(consoleRule); // Assigning the configuration to the logger LogManager.Configuration = config; }
//class TestLogManager //{ // // A Logger dispenser for the current configuration (Remember to call Flush on application exit) // public static LogFactory Instance { get { return instance.Value; } } // private static Lazy<LogFactory> instance = new Lazy<LogFactory>(BuildLogFactory); // private static LogFactory BuildLogFactory() // { // // Initialize LogFactory object // LogFactory logFactory = new LogFactory(); // // Initialize LogFactory Configuration // LoggingConfiguration config = new LoggingConfiguration(); // // Register the custom layout to show stack trace in logs // LayoutRenderer.Register("IndentationLayout", typeof(IndentationLayoutRenderer)); //no space // // Initialize File Target // var fileTarget = new FileTarget("File Target") // { // FileName = "${basedir}/logs/logTest.xml", // DeleteOldFileOnStartup = true, // Deletes old log file every time log is called. Set to true simply for testing purposes // KeepFileOpen = true, // Keeps file open regardless of logger status. (Increases performance) // OpenFileCacheTimeout = 30, // Max number of seconds file is kept open. (Increases performance, but the value will scale with the scope of logger implementation) // Layout = "${IndentationLayout} ${longdate} | ${level:uppercase=true} | ${logger} | ${message} | ${callsite:className=true:fileName=false:includeSourcePath=false:methodName=true}" //use IndentationLayout // }; // // Initialize the AsyncWrapper for the fileTarget // AsyncTargetWrapper fileWrapper = new AsyncTargetWrapper(); // fileWrapper.WrappedTarget = fileTarget; // fileWrapper.QueueLimit = 5000; // Limits number of requests in the request queue (Improves performance) // fileWrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Block; // Blocks incoming requests until request queue if has space // // Initialize the AsyncFlushTargetWrapper for the FileWrapper // AutoFlushTargetWrapper fileFlushWrapper = new AutoFlushTargetWrapper(); // fileFlushWrapper.WrappedTarget = fileWrapper; // // This condition controls when the log is flushed. Set the LogLevel to be equivalent to the maximum level specified in the targetRule // fileFlushWrapper.Condition = "level >= LogLevel.Info"; // // Adding the target to the config // config.AddTarget("file", fileFlushWrapper); // // Creating the Log Level rules for each target and adding them to the config // var fileRule = new LoggingRule("*", fileTarget); // fileRule.EnableLoggingForLevel(exceptionLevel); // fileRule.EnableLoggingForLevels(minLevel, maxLevel); // config.LoggingRules.Add(fileRule); // // Assigning the configuration to the logger // logFactory.Configuration = config; // return logFactory; // } //} private void SetupLoggingConfiguration() { // Intialize Config Object LoggingConfiguration config = new LoggingConfiguration(); LayoutRenderer.Register("IndentationLayout", typeof(IndentationLayoutRenderer)); //no space /* * // Initialize Console Target * var consoleTarget = new ColoredConsoleTarget("Console Target") * { * Layout = @"${time} ${longdate} ${uppercase: ${level}} ${logger} ${message} ${exception: format=ToString}" * }; * * // Initialize the AsyncWrapper for the ConsoleTarget * AsyncTargetWrapper consoleWrapper = new AsyncTargetWrapper(); * consoleWrapper.WrappedTarget = consoleTarget; * consoleWrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Block; * consoleWrapper.QueueLimit = 5000; * * // Initialize the AsyncFlushTargetWrapper for the ConsoleWrapper * AutoFlushTargetWrapper consoleFlushWrapper = new AutoFlushTargetWrapper(); * consoleFlushWrapper.WrappedTarget = consoleWrapper; * * // This condition controls when the log is flushed. Set the LogLevel to be equivalent to the maximum level specified in the targetRule * consoleFlushWrapper.Condition = "level >= LogLevel.Info"; * * // Adding the target to the config * config.AddTarget("console", consoleFlushWrapper); */ // Initialize File Target var fileTarget = new FileTarget("File Target") { FileName = @"${basedir}\logs\Robotics Automation Log.xml", DeleteOldFileOnStartup = true, // Deletes old log file every time log is called. Set to true simply for testing purposes KeepFileOpen = true, // Keeps file open regardless of logger status. (Increases performance) OpenFileCacheTimeout = 30, // Max number of seconds file is kept open. (Increases performance, but the value will scale with the scope of logger implementation) Layout = @"${IndentationLayout} ${date:format=yyyy-MM-dd HH\:mm\:ss} | ${level:uppercase=true} | ${message}" //use IndentationLayout }; //// XmlLayout Configuration //var XmlLayout = new XmlLayout(); //XmlLayout.IndentXml = true; //XmlLayout.ElementName = "Automated Test"; //XmlLayout.ElementValue = "Current Test"; // Unit Test name can be entered into here so each log entry will include Unit Test //XmlLayout.MaxRecursionLimit = 10; //XmlLayout.Attributes.Add(new XmlAttribute("Time", "${longdate}")); //XmlLayout.Attributes.Add(new XmlAttribute("Level", "${level:upperCase=true")); //XmlLayout.Elements.Add(new XmlElement("Output", "${message}")); //XmlLayout.Elements.Add(new XmlElement("Location", "${callsite:methodName=true}")); //fileTarget.Layout = XmlLayout; // Initialize the AsyncWrapper for the fileTarget AsyncTargetWrapper fileWrapper = new AsyncTargetWrapper(); fileWrapper.WrappedTarget = fileTarget; fileWrapper.QueueLimit = 5000; // Limits number of requests in the request queue (Improves performance) fileWrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Block; // Blocks incoming requests until request queue if has space // Initialize the AsyncFlushTargetWrapper for the FileWrapper AutoFlushTargetWrapper fileFlushWrapper = new AutoFlushTargetWrapper(); fileFlushWrapper.WrappedTarget = fileWrapper; /* * // Initiliaze Database Target * var dbTarget = new DatabaseTarget * { * ConnectionString = WhatINeed, * DBProvider = "MongoServer", * Name = "Mongo", * CommandText = * @"insert into dbo.Log ( * Logged, Level, Message, Username, Url, Logger, Callsite, Exception, Stacktrace, remoteAddress * ) values( * @Logged, @Level, @Message, @Username, @Url, @Logger, @Callsite, @Exception, @Stacktrace, @remoteAddress * );" * }; * * // Adding all database parameters to pass through Mongo * dbTarget.Parameters.Add(new DatabaseParameterInfo("@Logged", "${date}")); * dbTarget.Parameters.Add(new DatabaseParameterInfo("@Level", "${level}")); * dbTarget.Parameters.Add(new DatabaseParameterInfo("@Message", "${message}")); * dbTarget.Parameters.Add(new DatabaseParameterInfo("@Username", "${identity}")); * dbTarget.Parameters.Add(new DatabaseParameterInfo("@Url", "${I need this}")); * dbTarget.Parameters.Add(new DatabaseParameterInfo("@Logger", "${logger}")); * dbTarget.Parameters.Add(new DatabaseParameterInfo("@Callsite", "${callsite}")); * dbTarget.Parameters.Add(new DatabaseParameterInfo("@Exception", "${exception:format=toString}")); * dbTarget.Parameters.Add(new DatabaseParameterInfo("@StackTrace", "${stacktrace}")); * * // Add the target to the config * config.AddTarget("database", dbTarget); * * var databaseRule = new LoggingRule("DatabaseRule", LogLevel.Trace, dbTarget); * config.LoggingRules.Add(databaseRule); */ // This condition controls when the log is flushed. Set the LogLevel to be equivalent to the maximum level specified in the targetRule fileFlushWrapper.Condition = "level >= LogLevel.Info"; // Adding the target to the config config.AddTarget("file", fileFlushWrapper); // Creating the Log Level rules for each target and adding them to the config var fileRule = new LoggingRule("*", fileTarget); fileRule.EnableLoggingForLevel(exceptionLevel); fileRule.EnableLoggingForLevels(minLevel, maxLevel); config.LoggingRules.Add(fileRule); /* * var consoleRule = new LoggingRule("*", consoleTarget); * consoleRule.EnableLoggingForLevels(LogLevel.Trace, LogLevel.Info); * consoleRule.EnableLoggingForLevel(LogLevel.Error); * config.LoggingRules.Add(consoleRule); */ // Assigning the configuration to the logger LogManager.Configuration = config; }
public override void RuntimeInitialize(MethodBase method) { // var config = new LoggingConfiguration(); // var dbTarget = new DatabaseTarget(); // dbTarget.ConnectionString = @"Data Source=.;Initial Catalog=EmployeeManage;Integrated Security=True"; // dbTarget.CommandText = //@"INSERT INTO [AspNetCoreLog] (Logged, Level, Logger, Message, Exception) // VALUES(GETDATE(), @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 dbRule = new LoggingRule("*", LogLevel.Trace, dbTarget); // config.LoggingRules.Add(dbRule); // LogManager.Configuration = config; // Logger = LogManager.GetCurrentClassLogger(); // Logger.Info("WebCoreLogging Started"); var config = new LoggingConfiguration(); var dbTarget = new DatabaseTarget(); dbTarget.ConnectionString = CrossCutting.Caching.Caching.Instance.GetApplicationConfigs("DBConnection"); // dbTarget.ConnectionString = @"Data Source=.;Initial Catalog=EmployeeManage;Integrated Security=True"; dbTarget.CommandText = @"INSERT INTO [dbo].[LogsServiceConcrete] ([Application] ,[Logged] ,[Level] ,[UserName] ,[Message] ,[Machinename] ,[Logger] ,[Callsite] ,[Exception]) VALUES(@application, GETDATE(), @level,@username, @message, @machinename, @logger, @callSite, @exception)"; dbTarget.Parameters.Add(new DatabaseParameterInfo("@application", new NLog.Layouts.SimpleLayout(@"${appsetting:name=AppName:default=Unknown\: set AppName in appSettings}"))); dbTarget.Parameters.Add(new DatabaseParameterInfo("@username", new NLog.Layouts.SimpleLayout("${identity}"))); dbTarget.Parameters.Add(new DatabaseParameterInfo("@machinename", new NLog.Layouts.SimpleLayout("${machinename}"))); dbTarget.Parameters.Add(new DatabaseParameterInfo("@callSite", new NLog.Layouts.SimpleLayout("${callsite}"))); 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}"))); var asyncWrapper = new AsyncTargetWrapper(dbTarget, queueLimit: 10000, overflowAction: AsyncTargetWrapperOverflowAction.Grow); asyncWrapper.OptimizeBufferReuse = true; asyncWrapper.TimeToSleepBetweenBatches = 50; var autoFlushWrapper = new AutoFlushTargetWrapper(wrappedTarget: asyncWrapper); autoFlushWrapper.OptimizeBufferReuse = true; autoFlushWrapper.Condition = "level >= LogLevel.Warn"; autoFlushWrapper.AsyncFlush = false; var rule = new LoggingRule(loggerNamePattern: "*", minLevel: LogLevel.Trace, target: autoFlushWrapper); config.AddTarget("database", autoFlushWrapper); config.LoggingRules.Add(rule); //Activate configuration LogManager.Configuration = config; }