Visitar URL original
Updated API examples from SimpleConfigurator to LoggingConfiguration by snakefoot · Pull Request #5769 · NLog/NLog · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ protected void Application_Start(object sender, EventArgs e)
rule.Filter = "level >= LogLevel.Debug";
postfilteringTarget.Rules.Add(rule);

SimpleConfigurator.ConfigureForTargetLogging(aspnetBufferingTarget, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(aspnetBufferingTarget);
LogManager.Configuration = nlogConfig;
}

protected void Application_End(object sender, EventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Web;

using NLog;
using NLog.Config;
using NLog.Targets;

namespace SomeWebApplication
Expand All @@ -19,7 +20,9 @@ protected void Application_Start(Object sender, EventArgs e)
ASPNetTraceTarget target = new ASPNetTraceTarget();
target.Layout = "${logger} ${message}";

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System.Text;

class Example
{
Expand All @@ -11,18 +11,20 @@ static void Main(string[] args)
target.Layout = "${longdate} ${logger} ${message}";
target.FileName = "${basedir}/logs/logfile.txt";
target.KeepFileOpen = false;
target.Encoding = Encoding.UTF8;
target.Encoding = System.Text.Encoding.UTF8;

AsyncTargetWrapper wrapper = new AsyncTargetWrapper();
wrapper.WrappedTarget = target;
wrapper.QueueLimit = 5000;
wrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Discard;

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(wrapper, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(wrapper);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");

wrapper.Flush();
LogManager.Flush();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;

using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System.Diagnostics;

class Example
{
Expand All @@ -15,7 +13,10 @@ static void Main(string[] args)
AutoFlushTargetWrapper target = new AutoFlushTargetWrapper();
target.WrappedTarget = wrappedTarget;
target.Condition = "level >= LogLevel.Debug";
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;

using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System.Diagnostics;

class Example
{
Expand All @@ -16,9 +14,13 @@ static void Main(string[] args)
target.BufferSize = 100;
target.WrappedTarget = wrappedTarget;

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");

LogManager.Flush();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NLog;
using NLog.Config;
using NLog.Targets;

class Example
Expand All @@ -8,7 +9,9 @@ static void Main(string[] args)
ChainsawTarget target = new ChainsawTarget();
target.Address = "udp://localhost:4000";

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Trace("log message 1");
Expand All @@ -17,5 +20,7 @@ static void Main(string[] args)
logger.Warn("log message 4");
logger.Error("log message 5");
logger.Fatal("log message 6");

LogManager.Flush();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NLog;
using NLog.Win32.Targets;
using NLog.Config;
using NLog.Targets;

class Example
{
Expand All @@ -21,7 +22,10 @@ static void Main(string[] args)
ConsoleOutputColor.Yellow, // foreground color
ConsoleOutputColor.DarkBlue) // background color
);
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);

LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

// LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration("ColoredConsoleTargetRowHighlighting.nlog");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NLog;
using NLog.Win32.Targets;
using NLog.Config;
using NLog.Targets;

class Example
{
Expand All @@ -8,7 +9,9 @@ static void Main(string[] args)
ColoredConsoleTarget target = new ColoredConsoleTarget();
target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Trace("trace log message");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NLog;
using NLog.Win32.Targets;
using NLog.Config;
using NLog.Targets;

class Example
{
Expand All @@ -16,8 +17,9 @@ static void Main(string[] args)
ConsoleOutputColor.Cyan,
ConsoleOutputColor.NoChange));


NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Trace("trace log message abcdefghijklmnopq");
Expand Down
5 changes: 4 additions & 1 deletion examples/targets/Configuration API/Console/Simple/Example.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NLog;
using NLog.Config;
using NLog.Targets;

class Example
Expand All @@ -8,7 +9,9 @@ static void Main(string[] args)
ConsoleTarget target = new ConsoleTarget();
target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
5 changes: 4 additions & 1 deletion examples/targets/Configuration API/Database/MSSQL/Example.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NLog;
using NLog.Config;
using NLog.Targets;

class Example
Expand Down Expand Up @@ -35,7 +36,9 @@ static void Main(string[] args)
param.Layout = "${message}";
target.Parameters.Add(param);

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NLog;
using NLog.Config;
using NLog.Targets;

class Example
Expand All @@ -20,7 +21,9 @@ static void Main(string[] args)
target.Parameters.Add(new DatabaseParameterInfo("CALLSITE", "${callsite:filename=true}"));
target.Parameters.Add(new DatabaseParameterInfo("MESSAGE", "${message}"));

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NLog;
using NLog.Config;
using NLog.Targets;

class Example
Expand All @@ -18,7 +19,9 @@ static void Main(string[] args)
target.Parameters.Add(new DatabaseParameterInfo("CALLSITE", "${callsite:filename=true}"));
target.Parameters.Add(new DatabaseParameterInfo("MESSAGE", "${message}"));

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
7 changes: 4 additions & 3 deletions examples/targets/Configuration API/Debug/Simple/Example.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;

using NLog;
using NLog.Config;
using NLog.Targets;

class Example
Expand All @@ -10,7 +9,9 @@ static void Main(string[] args)
DebugTarget target = new DebugTarget();
target.Layout = "${message}";

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
7 changes: 4 additions & 3 deletions examples/targets/Configuration API/Debugger/Simple/Example.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;

using NLog;
using NLog.Config;
using NLog.Targets;

class Example
Expand All @@ -10,7 +9,9 @@ static void Main(string[] args)
DebuggerTarget target = new DebuggerTarget();
target.Layout = "${message}";

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
6 changes: 4 additions & 2 deletions examples/targets/Configuration API/EventLog/Simple/Example.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Win32.Targets;

class Example
{
Expand All @@ -11,7 +11,9 @@ static void Main(string[] args)
target.Log = "Application";
target.Layout = "${logger}: ${message} ${exception}";

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;

using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Targets.Compound;
using System.Diagnostics;
using NLog.Targets.Wrappers;

class Example
{
Expand All @@ -15,15 +13,16 @@ static void Main(string[] args)
FileTarget file2 = new FileTarget();
file2.FileName = "\\\\server2\\share\\file1.txt";


// write to server1, if it fails switch to server2
FallbackTarget target = new FallbackTarget();

target.ReturnToFirstOnSuccess = false;
target.Targets.Add(file1);
target.Targets.Add(file2);

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
Expand Down
9 changes: 4 additions & 5 deletions examples/targets/Configuration API/File/Archive1/Example.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Targets.Wrappers;

class Example
{
Expand All @@ -13,10 +13,9 @@ static void Main(string[] args)
target.ArchiveAboveSize = 10 * 1024; // archive files greater than 10 KB
target.ArchiveNumbering = FileTarget.ArchiveNumberingMode.Sequence;

// this speeds up things when no other processes are writing to the file
target.ConcurrentWrites = true;

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
LoggingConfiguration nlogConfig = new LoggingConfiguration();
nlogConfig.AddRuleForAllLevels(target);
LogManager.Configuration = nlogConfig;

Logger logger = LogManager.GetLogger("Example");

Expand Down
Loading