nlog 日志级别规则没有改变
Nlog log level rules not changed
例如这个 class 以编程方式初始化 NLog 配置:
public class NLogConfig
{
public NLogConfiguration(string traceFileName, LogLevel minLogleven, LogLevel maxLogLevel)
{
var config = new NLog.Config.LoggingConfiguration();
// Targets where to log to file
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = $"{traceFileName}.log" };
logfile.Layout = @"${date:format=HH\:mm\:ss}|${level}|${message} ${exception:format=tostring}";
// Rules for mapping loggers to targets
config.AddRule(minLogleven, maxLogLevel, logfile);
// Apply config
LogManager.Configuration = config;
}
}
现在,我用两种不同的 class 调用此配置,例如:
public class A
{
public A()
{
var nlogConfig = new NLogConfig("Trace1", LogLevel.Trace, LogLevel.Fatal);
}
}
public class B
{
public B()
{
var nlogConfig = new NLogConfig("Trace2", LogLevel.Info, LogLevel.Fatal);
}
}
现在的问题是,Nlog 配置不会采用这两个不同的日志级别规则,所以在那种情况下 Trace1.log
和 Trace2.log
日志都带有 trace
级别,而我希望 Trace1.log
以 Trace
级别登录,而 Trace2.log
以 info
级别登录。
如何解决此问题,以便我可以动态更改日志级别?
一开始我会尝试概述您的应用程序的总体设计,然后才会为您提供代码示例。
设计概览。
我们[开发人员]喜欢让事情尽可能简单。实现这一目标的方法之一是分离软件模块的职责。
在您的情况下 class A(或 B)不应该关心目标文件名是什么或日志级别。它应该只是记录记录,这就是全部!
但是你可以问如何用不同的设置写入不同的文件?为此,您应该使用 "targets" 和 "rules" 概念。基本上,target 只是一个日志目的地。它可以是文件、控制台等。规则是过滤 select 只有您需要的消息。它可以只是一个日志级别 or/and,甚至可以是 class 名称 or/and 的特殊过滤器。实际上,您在第一个示例中显示的配置是目标和规则的设置。
这种设计应该有助于分离和简化事物。我希望你明白我在说什么,否则想象一下当你需要更改项目中的日志记录设置时的模拟,其中有 100 个 classes 使用记录器。
可能的解决方案。
类 A 和 B 应该创建日志实例并使用它。
public class A
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public A()
{
Logger.Info("Class 'A' created.");
}
}
Nlog 配置通常在应用程序启动时完成,以便在应用程序生命周期内仅执行一次。
public static class Program
{
public static void Main()
{
LogConfiguration();
}
private static void LogConfiguration()
{
var config = new LoggingConfiguration()
// Targets where to log to: File and Console
var f1 = new FileTarget("file1") { FileName = "file1.txt" };
var f2 = new FileTarget("file2") { FileName = "file2.txt" };
// Rules for mapping loggers to targets
config.AddRule(LogLevel.Info, LogLevel.Fatal, f1);
config.AddRule(LogLevel.Debug, LogLevel.Fatal, f2);
// Apply config
NLog.LogManager.Configuration = config;
}
}
可能的特征。
从您的代码看来,您可能只想将来自 class B 的消息写入 "file2.txt"。这也是可能的。您需要在规则创建期间指定记录器名称,如此处所示。小提示:您要使用的名称是您的完整名称class。
从 this 例子中你会明白我在说什么。
例如这个 class 以编程方式初始化 NLog 配置:
public class NLogConfig
{
public NLogConfiguration(string traceFileName, LogLevel minLogleven, LogLevel maxLogLevel)
{
var config = new NLog.Config.LoggingConfiguration();
// Targets where to log to file
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = $"{traceFileName}.log" };
logfile.Layout = @"${date:format=HH\:mm\:ss}|${level}|${message} ${exception:format=tostring}";
// Rules for mapping loggers to targets
config.AddRule(minLogleven, maxLogLevel, logfile);
// Apply config
LogManager.Configuration = config;
}
}
现在,我用两种不同的 class 调用此配置,例如:
public class A
{
public A()
{
var nlogConfig = new NLogConfig("Trace1", LogLevel.Trace, LogLevel.Fatal);
}
}
public class B
{
public B()
{
var nlogConfig = new NLogConfig("Trace2", LogLevel.Info, LogLevel.Fatal);
}
}
现在的问题是,Nlog 配置不会采用这两个不同的日志级别规则,所以在那种情况下 Trace1.log
和 Trace2.log
日志都带有 trace
级别,而我希望 Trace1.log
以 Trace
级别登录,而 Trace2.log
以 info
级别登录。
如何解决此问题,以便我可以动态更改日志级别?
一开始我会尝试概述您的应用程序的总体设计,然后才会为您提供代码示例。
设计概览。
我们[开发人员]喜欢让事情尽可能简单。实现这一目标的方法之一是分离软件模块的职责。 在您的情况下 class A(或 B)不应该关心目标文件名是什么或日志级别。它应该只是记录记录,这就是全部! 但是你可以问如何用不同的设置写入不同的文件?为此,您应该使用 "targets" 和 "rules" 概念。基本上,target 只是一个日志目的地。它可以是文件、控制台等。规则是过滤 select 只有您需要的消息。它可以只是一个日志级别 or/and,甚至可以是 class 名称 or/and 的特殊过滤器。实际上,您在第一个示例中显示的配置是目标和规则的设置。 这种设计应该有助于分离和简化事物。我希望你明白我在说什么,否则想象一下当你需要更改项目中的日志记录设置时的模拟,其中有 100 个 classes 使用记录器。
可能的解决方案。
类 A 和 B 应该创建日志实例并使用它。
public class A
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public A()
{
Logger.Info("Class 'A' created.");
}
}
Nlog 配置通常在应用程序启动时完成,以便在应用程序生命周期内仅执行一次。
public static class Program
{
public static void Main()
{
LogConfiguration();
}
private static void LogConfiguration()
{
var config = new LoggingConfiguration()
// Targets where to log to: File and Console
var f1 = new FileTarget("file1") { FileName = "file1.txt" };
var f2 = new FileTarget("file2") { FileName = "file2.txt" };
// Rules for mapping loggers to targets
config.AddRule(LogLevel.Info, LogLevel.Fatal, f1);
config.AddRule(LogLevel.Debug, LogLevel.Fatal, f2);
// Apply config
NLog.LogManager.Configuration = config;
}
}
可能的特征。
从您的代码看来,您可能只想将来自 class B 的消息写入 "file2.txt"。这也是可能的。您需要在规则创建期间指定记录器名称,如此处所示。小提示:您要使用的名称是您的完整名称class。 从 this 例子中你会明白我在说什么。