我昨天开始学习NLog。另一个问题(配置NLog在XML输出中记录异常?)的答案是,您可以使用NLog.config
文件将输出配置为XML数据。
我把这个文件放在我的应用程序文件夹中:
<?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">
<target name="xmlFile" xsi:type="File" fileName="d:\${shortdate}.log" >
<layout xsi:type="XmlLayout" includeAllProperties="false" elementName='logevent'>
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<element name="message" value="${message}" />
<element name="exception_type" layout="${exception:format=Type}"/>
</layout>
</target>
</nlog>
并且,我在测试控制台应用程序中有虚拟代码:
namespace TestConsoleNlog
{
internal class Program
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
if(Logger != null)
{
Logger.Info("This is a note");
try
{
Logger.Info("Hello world");
System.Console.ReadKey();
}
catch (Exception ex)
{
Logger.Error(ex, "Goodbye cruel world");
}
}
}
}
}
我希望它在D驱动器的根目录下创建/更新一个日志文件,但是没有文件。为什么?
并且,基于问题(是否有一种方法将NLog。配置信息在我的app.config文件?)我试着把它放在app.config
中:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog>
<targets>
<target name="xmlFile" type="File" fileName="d:\${shortdate}.log" >
<layout type="XmlLayout" includeAllProperties="false" elementName='logevent'>
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<element name="message" value="${message}" />
<element name="exception_type" layout="${exception:format=Type}"/>
</layout>
</target>
</targets>
</nlog>
</configuration>
没有快乐。
I have try:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog internalLogLevel="Trace">
<targets>
<target name="xmlFile" type="File" fileName="d:\${shortdate}.log" >
<layout type="XmlLayout" includeAllProperties="false" elementName='logevent'>
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<element name="message" value="${message}" />
<element name="exception_type" layout="${exception:format=Type}"/>
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="xmlFile" />
</rules>
</nlog>
</configuration>
但仍然没有快乐。
我添加了手动代码来做内部日志记录,它是这样说的:
2022-12-28 20:08:03.4444 Error Error has been raised. Exception: System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element. (D:My DebugTestConsoleNlogTestConsoleNlogbinDebugTestConsoleNlog.exe.Config line 6)
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
--- End of inner exception stack trace ---
at System.Configuration.ConfigurationManager.PrepareConfigSystem()
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at NLog.Config.LoggingConfigurationWatchableFileLoader.TryLoadFromAppConfig()
2022-12-28 20:08:03.4564 Debug No file exists at candidate config file location: D:My DebugTestConsoleNlogTestConsoleNlogbinDebugTestConsoleNlog.exe.nlog
2022-12-28 20:08:03.4564 Debug No file exists at candidate config file location: D:My DebugTestConsoleNlogTestConsoleNlogbinDebugNLog.config
2022-12-28 20:08:03.4564 Debug No file exists at candidate config file location: D:My DebugTestConsoleNlogTestConsoleNlogbinDebugNLog.dll.nlog
2022-12-28 20:08:05.6805 Debug No file exists at candidate config file location: D:My DebugTestConsoleNlogTestConsoleNlogbinDebugTestConsoleNlog.exe.nlog
2022-12-28 20:08:05.6805 Debug No file exists at candidate config file location: D:My DebugTestConsoleNlogTestConsoleNlogbinDebugNLog.config
2022-12-28 20:08:05.6805 Debug No file exists at candidate config file location: D:My DebugTestConsoleNlogTestConsoleNlogbinDebugNLog.dll.nlog
2022-12-28 20:08:05.6805 Info Shutdown() called. Logger closing...
2022-12-28 20:08:05.6805 Info Logger has been closed down.
2022-12-28 20:08:05.6865 Info AppDomain Shutting down. LogFactory closing...
2022-12-28 20:08:05.6865 Info LogFactory has been closed.
app.config
中的<configSections>
必须在顶部(高于<startup>
):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<nlog>
<targets>
<target name="xmlFile" type="File" fileName="d:\${shortdate}.log" >
<layout type="XmlLayout" includeAllProperties="false" elementName='logevent'>
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<element name="message" value="${message}" />
<element name="exception_type" value="${exception:format=Type}"/>
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="xmlFile" />
</rules>
</nlog>
</configuration>
还记得包含NLog日志<rules>
来执行从NLog记录器输出到目标NLog目标的映射。