I started learning NLog yesterday. An answer to another question (Configuring NLog to log exceptions in an XML output?) said that you can use a NLog.config
file to configure output as XML data.
I placed this file in my application folder:
<?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>
And, I have dummy code in a test console application:
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");
}
}
}
}
}
I expected it to create / update a log file in the root of D drive but there is no file. Why?
And, based on the question (Is there a way to put NLog.config information inside of my app.config file?) I tried to put it in the app.config
instead:
<?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>
No joy.
I have tried:
<?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>
But no joy still.
I added manual code to do internal logging and this is what it says:
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.
CodePudding user response:
The <configSections>
in app.config
must be at the top (above <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>
Also remember to include NLog logging <rules>
to perform mapping from NLog Logger-output to destination NLog Target.