![](/img/trans.png)
[英]Set ApplicationData folder on app.config and get it on Nlog.config
[英]Logging to XML format failing for NLog.config file or app.config
我昨天開始學習 NLog。 另一個問題的答案( Configuring NLog to log exceptions in an XML output? )說您可以使用NLog.config
文件將 output 配置為 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.config 信息放入我的 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>
沒有快樂。
我試過了:
<?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 Debug\TestConsoleNlog\TestConsoleNlog\bin\Debug\TestConsoleNlog.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 Debug\TestConsoleNlog\TestConsoleNlog\bin\Debug\TestConsoleNlog.exe.nlog
2022-12-28 20:08:03.4564 Debug No file exists at candidate config file location: D:\My Debug\TestConsoleNlog\TestConsoleNlog\bin\Debug\NLog.config
2022-12-28 20:08:03.4564 Debug No file exists at candidate config file location: D:\My Debug\TestConsoleNlog\TestConsoleNlog\bin\Debug\NLog.dll.nlog
2022-12-28 20:08:05.6805 Debug No file exists at candidate config file location: D:\My Debug\TestConsoleNlog\TestConsoleNlog\bin\Debug\TestConsoleNlog.exe.nlog
2022-12-28 20:08:05.6805 Debug No file exists at candidate config file location: D:\My Debug\TestConsoleNlog\TestConsoleNlog\bin\Debug\NLog.config
2022-12-28 20:08:05.6805 Debug No file exists at candidate config file location: D:\My Debug\TestConsoleNlog\TestConsoleNlog\bin\Debug\NLog.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 Logger-output 到目標 NLog Target 的映射。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.