C# 编程NLog配置isn';行不通
我正在使用Nlog设置一个测试应用程序,我希望配置可以通过代码进行编辑。我已经正确地设置了配置,但是我实际上看不到任何日志记录。我不确定这是因为我没有正确地编码它,还是因为我没有正确地实现它。我没有看到正在创建的文件,也没有看到彩色控制台。由于没有太多关于编程配置的文档,因此非常感谢您的帮助C# 编程NLog配置isn';行不通,c#,nlog,C#,Nlog,我正在使用Nlog设置一个测试应用程序,我希望配置可以通过代码进行编辑。我已经正确地设置了配置,但是我实际上看不到任何日志记录。我不确定这是因为我没有正确地编码它,还是因为我没有正确地实现它。我没有看到正在创建的文件,也没有看到彩色控制台。由于没有太多关于编程配置的文档,因此非常感谢您的帮助 using System; using NLog; using NLog.Targets; using NLog.Targets.Wrappers; using NLog.Config; namespac
using System;
using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;
using NLog.Config;
namespace NLogArchitecture
{
class ApplicationFramework
{
public static Logger log = LogManager.GetCurrentClassLogger();
public static int sum = 0;
public static void Main()
{
SetupLoggingConfiguration();
F1();
F2();
F3();
F4();
}
public static void F1()
{
int[] array1 = new int[5] { 1, 2, 3, 4, 5 };
for (int i = 0; i > array1.Length; i++)
{
sum += array1[i];
}
log.Trace("The sum of array1 is: " + sum);
}
public static void F2()
{
int sumException = 0;
try
{
sumException = sum / 0;
}
catch(Exception ex)
{
log.Error("Invalid operation: " + ex);
}
}
public static void F3()
{
sum = sum + 3;
log.Debug("Consider a different syntax");
}
public static void F4()
{
if (sum > 12) log.Info("The sum has been calculated well");
if (sum <= 10) log.Info("The sum has been calculated incorrectly");
}
public static void SetupLoggingConfiguration()
{
// 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 = "@C:/logs/log.txt",
KeepFileOpen = false,
Layout = @"${time} ${longdate} ${uppercase: ${level}} ${logger} ${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("FileRule", fileTarget);
fileRule.EnableLoggingForLevels(LogLevel.Trace, LogLevel.Info);
fileRule.EnableLoggingForLevel(LogLevel.Error);
config.LoggingRules.Add(fileRule);
var consoleRule = new LoggingRule("ConsoleRule", consoleTarget);
consoleRule.EnableLoggingForLevels(LogLevel.Trace, LogLevel.Info);
consoleRule.EnableLoggingForLevel(LogLevel.Error);
config.LoggingRules.Add(consoleRule);
// Assigning the configuration to the logger
LogManager.Configuration = config;
}
}
}
使用系统;
使用NLog;
使用非直瞄目标;
使用NLog.Targets.Wrappers;
使用NLog.Config;
命名空间体系结构
{
类应用程序框架
{
公共静态记录器log=LogManager.GetCurrentClassLogger();
公共静态整数和=0;
公共静态void Main()
{
SetupLoggingConfiguration();
F1();
F2();
F3();
F4();
}
公共静态无效F1()
{
int[]array1=新的int[5]{1,2,3,4,5};
对于(int i=0;i>array1.Length;i++)
{
总和+=阵列1[i];
}
log.Trace(“数组1的和为:“+sum”);
}
公共静态无效F2()
{
int-sumException=0;
尝试
{
sumException=sum/0;
}
捕获(例外情况除外)
{
日志错误(“无效操作:+ex”);
}
}
公共静态无效F3()
{
总和=总和+3;
Debug(“考虑不同的语法”);
}
公共静态无效F4()
{
如果(总和>12)log.Info(“总和计算得很好”);
如果(sum您的问题在您的LoggingRule模式中。NLog不知道什么是“ConsoleRule”或“FileRule”。由于您使用的是默认模式,因此您的logger名称中没有类似的匹配模式
var consoleRule = new LoggingRule("ConsoleRule", consoleTarget);
var fileRule = new LoggingRule("FileRule", fileTarget);
将其更改为“*”以匹配全部或为记录器提供一个名称以匹配规则
var consoleRule = new LoggingRule("*", consoleTarget);
var fileRule = new LoggingRule("*", fileTarget);
您的问题出现在LoggingRule模式中。NLog不知道什么是“ConsoleRule”或“FileRule”。由于您使用的是默认模式,因此您的logger名称中没有类似的匹配模式
var consoleRule = new LoggingRule("ConsoleRule", consoleTarget);
var fileRule = new LoggingRule("FileRule", fileTarget);
将其更改为“*”以匹配全部或为记录器提供一个名称以匹配规则
var consoleRule = new LoggingRule("*", consoleTarget);
var fileRule = new LoggingRule("*", fileTarget);
在配置代码的末尾尝试LogManager.reconfigeExistingLoggers()
。嘿,艾米,这似乎不起作用。可能是我的方法实现有问题吗?尝试LogManager.reconfigeExistingLoggers()
在您的配置代码末尾。嘿,艾米,这似乎不起作用。可能是我在方法中的实现有问题吗?谢谢,就是这样!我真的很感激。谢谢,就是这样!我真的很感激。