标签:amp console ase txt toc win keep cep statement
This describes NLog for .NET Framework (.NET ver. 3.5 - 4.8) and .NET Core (NetStandard 1.3+)
NLog can be setup with the following steps:
After having setup NLog, then make sure to explore:
If something is not working as expected then check the Troubleshooting section.
If wanting to use Microsoft Extension Logging (MEL) then check .NET Core and ASP.NET.Core tutorials.
NLog will only produce output if having configured one (or more) NLog targets.
NLog can be configured using XML by adding a NLog.config
file to your application project (File Properties: Copy If newer). NLog will automatically load the NLog.config by searching multiple file locations. This is a simple example of the content for NLog.config
:
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets> <target name="logfile" xsi:type="File" fileName="file.txt" /> <target name="logconsole" xsi:type="Console" /> </targets> <rules> <logger name="*" minlevel="Info" writeTo="logconsole" /> <logger name="*" minlevel="Debug" writeTo="logfile" /> </rules> </nlog>
To configure programmatically, then one can do this in code:
var config = new NLog.Config.LoggingConfiguration(); // Targets where to log to: File and Console var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "file.txt" }; var logconsole = new NLog.Targets.ConsoleTarget("logconsole"); // Rules for mapping loggers to targets config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole); config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile); // Apply config NLog.LogManager.Configuration = config;
The rules redirects logger output to the wanted output targets. The rules also provides filtering options to output only relevant logevents. Usually based on logger-name and loglevel (Ex. name="*" minlevel="Trace"
means everything).
See also Configuration File or Configure from code
See also Available NLog Targets for output.
public static class Program
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
public void Main()
{
try
{
Logger.Info("Hello world");
System.Console.ReadKey();
}
catch (Exception ex)
{
Logger.Error(ex, "Goodbye cruel world");
}
}
}
The Logger can write messages with different LogLevels, which is used by the logging-rules (See minLevel
in configuration example above) so only relevant messages are redirected to the wanted targets. The LogLevel identifies how important/detailed the message is. NLog can route log messages based primarily on their logger name and log level.
NLog supports the following log levels:
Trace
- very detailed logs, which may include high-volume information such as protocol payloads. This log level is typically only enabled during developmentDebug
- debugging information, less detailed than trace, typically not enabled in production environment.Info
- information messages, which are normally enabled in production environmentWarn
- warning messages, typically for non-critical issues, which can be recovered or which are temporary failuresError
- error messages - most of the time these are Exceptions
Fatal
- very serious errors!The logger is not tied to a specific target. The messages written to one logger can reach multiple targets based on the logging-rules configuration. Maintaining this separation lets you keep logging statements in your code and easily change how and where the logs are written, just by updating the configuration in one place. See also Filtering log messages.
NLog is also supported by many logging abstractions like Microsoft Extension Logging and LibLog.
标签:amp console ase txt toc win keep cep statement
原文地址:https://www.cnblogs.com/chucklu/p/13226034.html