I'm trying to setup NLog to send mail and use my system settings (with a pickup directory) as documented here
Here's my NLog configuration
<nlog internalLogLevel="Trace" internalLogFile="C:\NLogInternal.log" throwExceptions="true" autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="c:\backupservice.log.txt" layout="${longdate} ${callsite} ${level} ${message}"/>
<target name="console" xsi:type="Console" />
<target xsi:type="EventLog"
name="event"
layout="${longdate} ${callsite} ${level} ${message}"
source="BackupService"
eventId="898"
log="BackupService"
/>
<target xsi:type="Mail"
name="email"
useSystemNetMailSettings="True"
layout="${longdate} ${callsite} ${level} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Fatal" writeTo="email" />
<logger name="*" minLevel="Info" writeTo="event" />
<logger name="*" minLevel="Debug" writeTo="console" />
</rules>
Here is my mail setting:
<system.net>
<mailSettings>
<smtp from="backup#[COMPANY].com" deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\testmail\Pickup" />
<network host="mail.[COMPANY].com" password="[PASSWORD]" userName="[EMAIL_ADDRESS]" />
</smtp>
</mailSettings>
Here is the output from NLog's internal logging:
** SNIP **
2013-06-20 17:41:03.8368 Debug Setting 'MailTarget.name' to 'email'
2013-06-20 17:41:03.8368 Debug Setting 'MailTarget.useSystemNetMailSettings' to 'True'
2013-06-20 17:41:03.8688 Error Error System.NotSupportedException: Parameter useSystemNetMailSettings not supported on MailTarget
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object o, String name, String value, ConfigurationItemFactory configurationItemFactory)
at NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType)
at NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement)
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String baseDirectory)
at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String baseDirectory)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)...
2013-06-20 17:41:03.8688 Error ConfigSectionHandler error: NLog.NLogConfigurationException: Exception occurred when loading configuration from C:\Projects\Fee\WindowsServices\BackupService\BackupService\bin\Debug\BackupService.vshost.exe.Config ---> System.NotSupportedException: Parameter useSystemNetMailSettings not supported on MailTarget
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object o, String name, String value, ConfigurationItemFactory configurationItemFactory)
at NLog.Config.XmlLoggingConfiguration.ConfigureObjectFromAttributes(Object targetObject, NLogXmlElement element, Boolean ignoreType)
at NLog.Config.XmlLoggingConfiguration.ParseTargetElement(Target target, NLogXmlElement targetElement)
at NLog.Config.XmlLoggingConfiguration.ParseTargetsElement(NLogXmlElement targetsElement)
at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String baseDirectory)
at NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String baseDirectory)
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
--- End of inner exception stack trace ---
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
at NLog.Config.XmlLoggingConfiguration..ctor(XmlElement element, String fileName)
at NLog.Config.ConfigSectionHandler.System.Configuration.IConfigurationSectionHandler.Create(Object parent, Object configContext, XmlNode section)
I'm not sure what I'm doing wrong or if it's a bug in Nlog.
NB: I've tried both useSystemNetMailSettings and UseSystemNetMailSettings.
So I didn't get NLog to work specifically through NLog but I used a work around. NLog has a "MethodCall" target that I used like so:
<target name="sendmail" xsi:type="MethodCall" className="BackupLib.Email, BackupLib" methodName="Send">
<parameter layout="backupservice#[COMPANY].com" />
<parameter layout="backups#[COMPANY].com" />
<parameter layout="FATAL ERROR: Backup Service on ${machinename}" />
<parameter layout="${longdate} - ${callsite} - ${message}" />
</target>
</targets>
<rules>
<logger name="*" minLevel="Fatal" writeTo="sendmail" />
<logger name="*" minLevel="Info" writeTo="event" />
<logger name="*" minLevel="Debug" writeTo="console" />
</rules>
This allowed me to call a static method (BackupLib.Email.Send()) that sent out an email for me. Simple yet effective and since I used the SMTPClient baked into .NET it used my system email settings!
Glad I finally got this.
If you have the extension NLog.MailKit installed and you try to use the useSystemNetMailSettings parameter within your target, this will causes the error Parameter useSystemNetMailSettings not supported on MailTarget.
Uninstalling the NLog.MailKit package fixed this for me!
The Nlog.MailKit package is only required if you are using NetStandard1.X.
Related
I have implemented NLog for .NET Core 3.1 MVC web app and API projects. The nlog.config file for both of them is nearly identical. I've carefuly checked this and the only difference is the database table name and file name that they log to.
I'm successfully suppressing non-essential Microsoft logs from the API project, but am only partially able to do so for the web app. Specifically, Trace and Info logs by Microsoft that deal with making calls to the API using HttpClient appear in the file and database:
You can see that only one of the logs is a log that I actually wrote, and the rest are were automatically logged by Microsoft.
I'm not sure what to try here, but I've looked at the internal logs and saw nothing out of the ordinary.
I want to know how can I suppress the additional Microsoft logs and what's wrong with my current configuration?
NuGet packages I have installed (identical for both apps):
nlog.config of the web app project:
<?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"
autoReload="true"
throwConfigExceptions="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-CNC_WebUI-nlog.txt">
<extensions>
<add assembly="Nlog.Extensions.Logging"/>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target xsi:type="File" name="CNC_WebUIFile" fileName="c:\temp\nlog-CNC_WebUIFile-${shortdate}.log"
layout="${aspnet-traceidentifier}|${configsetting:AppSettings.NlogConnection.AppName}|${event-properties:ObjectID}|${date}|${uppercase:${level}}|${message} ${exception:format=tostring}|${logger}|${callsite:filename=false}|${exception:toString}" />
<target name="databaseLogger" xsi:type="Database"
dbProvider="sqlserver"
dbHost="${configsetting:AppSettings.NlogConnection.DbHost}"
dbDatabase="${configsetting:AppSettings.NlogConnection.Database}"
dbUserName="${configsetting:AppSettings.NlogConnection.User}"
dbPassword="${configsetting:AppSettings.NlogConnection.Password}" >
<commandText>
INSERT INTO dbo.CNC_WebUILogs (
CorrelationId, Application, ObjectID, Logged, Level, Message,
Logger, CallSite, Exception
) VALUES (
#CorrelationId, #Application, #ObjectID, #Logged, #Level, #Message,
#Logger, #Callsite, #Exception
);
</commandText>
<parameter name="#correlationId" layout="${aspnet-traceidentifier}" />
<parameter name="#application" layout="${configsetting:AppSettings.NlogConnection.AppName}" />
<parameter name="#ObjectID" layout="${event-properties:ObjectID}" />
<parameter name="#logged" layout="${date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#message" layout="${message}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#callSite" layout="${callsite:filename=false}" />
<parameter name="#exception" layout="${exception:toString}" />
</target>
</targets>
<rules>
<logger name="Microsoft.*" levels="Warn,Error,Fatal" writeTo="databaseLogger,CNC_WebUIFile"></logger>
<logger name="Microsoft.*" minlevel="Trace" final="true" />
<logger name="*" minlevel="Trace" writeTo="CNC_WebUIFile" />
<logger name="*" minlevel="${configsetting:AppSettings.NlogConnection.LogLevel}" writeTo="databaseLogger" />
</rules>
</nlog>
One of my repos that had their API call activity logged by Microsoft:
public class MfgrRepo : IMfgrRepo
{
private readonly IHttpClientFactory _clientFactory;
public MfgrRepo(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task<List<MfgrDto>> Get()
{
HttpClient client = _clientFactory.CreateClient(HttpClientConfigNames.CNC);
List<MfgrDto> models = new List<MfgrDto>();
try
{
HttpResponseMessage response = await client.GetAsync("api/Mfgrs");
models.AddRange(await response.Content.ReadFromJsonAsync<List<MfgrDto>>());
}
catch (Exception ex)
{
throw ex;
}
return models;
}
}
You can configure the log levels that the M.E.Logging system uses through the appsettings.json file. This also allows you to configure the types per namespace, so you can make the System.Net.Http namespace silent that way:
{
"Logging": {
"LogLevel": {
"System.Net.Http": "Warning"
}
}
}
You already have one blackhole here:
<rules>
<logger name="Microsoft.*" levels="Warn,Error,Fatal" writeTo="databaseLogger,CNC_WebUIFile"></logger>
<logger name="Microsoft.*" minlevel="Trace" final="true" /> <!-- Black Hole -->
<logger name="*" minlevel="Trace" writeTo="CNC_WebUIFile" />
<logger name="*" minlevel="${configsetting:AppSettings.NlogConnection.LogLevel}" writeTo="databaseLogger" />
</rules>
I would just configure an additional "blackhole" like this:
<rules>
<logger name="Microsoft.*" maxLevel="Info" final="true" /> <!-- Black Hole 1 -->
<logger name="System.Net.Http.*" maxLevel="Info" final="true" /> <!-- Black Hole 2 -->
<logger name="*" minlevel="Trace" writeTo="CNC_WebUIFile" />
<logger name="*" minlevel="${configsetting:AppSettings.NlogConnection.LogLevel}" writeTo="databaseLogger" />
</rules>
I'm using nlog to trace errors by email - all the configuration is stored in web.config. I'm trying to use appSetting for key enableSsl in nlog mail section but it is not taken into account for this specific key (but it works fine for other keys like: smtpServer, from, to, ...)
The following package are installed:
NLog<br/>
NLog.Extended // required to use appSetting<br/>
Nlog.Targets.Syslog<br/>
Nlog.Web<br/>
Documentation:
Mail-target
AppSetting-Layout-Renderer
Environment is C# .NET
It crashes with: enableSsl="${appsetting:Mail.Ssl.Enabled}"
It works fine with: enableSsl="False"
web.config :
<appSettings>
<add key="Mail.Server" value="mail.myserver" />
<add key="Mail.Server.Port" value="25" />
<add key="Mail.Ssl.Enabled" value="False" />
<add key="Mail.Credentials.User" value="" />
</appSettings>
...
<nlog ..>
<targets>
<target name="mail" xsi:type="Mail" html="true" replaceNewlineWithBrTagInHtml="true" enableSsl="${appsetting:Mail.Ssl.Enabled}" smtpServer="${appsetting:Mail.Server}" from="${appsetting:Mail.From}" to="${appsetting:Mail.To.Error}" subject="Error on my app" layout="${message}..." />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
<!--<logger name="*" minlevel="Info" writeTo="syslog" />-->
<logger name="*" minlevel="Error" writeTo="mailBuffer" />
<logger name="*" minlevel="Info" writeTo="database" />
</rules>
</nlog>
nlog-internal.log :
Error when setting '${appsetting:Mail.Ssl.Enabled}' on attibute
'enableSsl' Exception: NLog.NLogConfigurationException: Error when
setting property 'EnableSsl' on Mail Target[mail] --->
System.FormatException: ${appsetting:Mail.Ssl.Enabled} is not a valid
value for Boolean. ---> System.FormatException: String was not
recognized as a valid Boolean.
at System.Boolean.Parse(String value)
at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext
context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext
context, CultureInfo culture, Object value)
at NLog.Internal.PropertyHelper.TryTypeConverterConversion(Type type, String value, Object& newValue)
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory
configurationItemFactory)
--- End of inner exception stack trace ---
at NLog.Internal.PropertyHelper.SetPropertyFromString(Object obj, String propertyName, String value, ConfigurationItemFactory
configurationItemFactory)
at NLog.Config.LoggingConfigurationParser.ConfigureObjectFromAttributes(Object
targetObject, ILoggingConfigurationElement element, Boolean
ignoreType)
enableSsl isn't of type Layout, but of type boolean. (see docs).
This means it isn't layoutable and so you can't use layout renderers for this value.
You could (as exception) variables in the config, as those are inlined when reading the config. e.g.
<variable name="myvar" value="true" />
...
<target name="mail" xsi:type="Mail" enableSsl="${myvar}" ../>
If you need to change the config dynamically, you need some code.
I've tried to implement NLog in my application. However, for some reason nothing is being written to the database. I have tried to debug a simple info and noticed that I have this error :
- DeclaringMethod '((System.RuntimeType)log._loggerType).DeclaringMethod' threw an exception of type 'System.InvalidOperationException' System.Reflection.MethodBase {System.InvalidOperationException}
Not sure how this is relevant.
I have switched on internal debugging as well and have not received any noticeable errors. This is the internal debug log:
2018-07-23 00:25:37.1679 Trace Scanning Property Layout ''${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}'' NLog.Layouts
2018-07-23 00:25:37.1679 Trace Scanning SimpleLayout ''${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}''
2018-07-23 00:25:37.1868 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.1868 Trace Scanning Property Layout ''${logger}'' NLog.Layouts
2018-07-23 00:25:37.2058 Trace Scanning SimpleLayout ''${logger}''
2018-07-23 00:25:37.2148 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.2148 Trace Scanning Property Layout ''${callsite}'' NLog.Layouts
2018-07-23 00:25:37.2367 Trace Scanning SimpleLayout ''${callsite}''
2018-07-23 00:25:37.2457 Trace Scanning DatabaseParameterInfo 'NLog.Targets.DatabaseParameterInfo'
2018-07-23 00:25:37.2457 Trace Scanning Property Layout ''${exception:tostring}'' NLog.Layouts
2018-07-23 00:25:37.2687 Trace Scanning SimpleLayout ''${exception:tostring}''
2018-07-23 00:25:37.2887 Trace Database Target[database] has 16 layouts
2018-07-23 00:25:37.3007 Trace FindReachableObject<System.Object>:
2018-07-23 00:25:37.3117 Trace Scanning SimpleLayout ''Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-ManagementStudio-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ManagementStudio-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c.mdf''
2018-07-23 00:25:37.3117 Trace Scanning Property Renderers 'System.Collections.ObjectModel.ReadOnlyCollection`1[NLog.LayoutRenderers.LayoutRenderer]' System.Collections.ObjectModel
2018-07-23 00:25:37.3296 Trace Scanning LiteralLayoutRenderer 'Layout Renderer: ${literal}'
2018-07-23 00:25:37.4832 Debug Watching path 'C:\Users\amosa\Source\Repos\ManagementStudio\ManagementStudio' filter 'NLog.config' for changes.
2018-07-23 00:25:37.4936 Info Configuration initialized.
2018-07-23 00:25:37.5095 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.5.7.7958. Product version: 4.5.7.
2018-07-23 00:25:37.5095 Debug Targets for ManagementStudio.Controllers.UsersController by level:
2018-07-23 00:25:37.5304 Debug Trace =>
2018-07-23 00:25:37.5398 Debug Debug =>
2018-07-23 00:25:37.5398 Debug Info =>
2018-07-23 00:25:37.5598 Debug Warn =>
2018-07-23 00:25:37.5768 Debug Error =>
2018-07-23 00:25:37.5768 Debug Fatal =>
Here is my nlog.config file. I have created the database table based on what this config shows:
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Trace" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<target name="database" xsi:type="Database">
<connectionStringName>DefaultConnection</connectionStringName>
<commandText>
insert into dbo.AspNetEventLogs (
Application, Logged, Level, Message,
Username,
ServerName, Port, Url, Https,
ServerAddress, RemoteAddress,
Logger, CallSite, Exception
) values (
#Application, #Logged, #Level, #Message,
#Username,
#ServerName, #Port, #Url, #Https,
#ServerAddress, #RemoteAddress,
#Logger, #Callsite, #Exception
);
</commandText>
<parameter name="#application" layout="${appsetting:name=appName}" />
<parameter name="#logged" layout="${date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#message" layout="${message}" />
<parameter name="#username" layout="${identity}" />
<parameter name="#serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
<parameter name="#port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
<parameter name="#url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
<parameter name="#https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
<parameter name="#serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
<parameter name="#remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#callSite" layout="${callsite}" />
<parameter name="#exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="database" minlevel="Debug" writeTo="database" />
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>
The name of the logger-rules is actually a wildcard for matching logger-names. Try changing to a star instead:
<rules>
<logger name="*" minlevel="Debug" writeTo="database" />
<rules>
See also https://github.com/NLog/NLog/wiki/Configuration-file#rules
I have a problem with NLog.
I have configured the NLog in the file Nlog.config as following:
<?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"
autoReload="true"
throwExceptions="true"
internalLogLevel="Trace"
internalLogFile="d:\temp\nlog-internal.log">
<target xsi:type="File" name="log" fileName="${basedir}\logs\${date:format=dd}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<rules>
<logger name="*" minlevel="Debug" writeTo="log" />
</rules>
</nlog>
And I get an error that target log is not found. I really don't know what I can do, maybe someone had similar problem.
I'm using .NET 4.5.
The Nlog.config is in the main directory of the project.
I set up the permissions to full access in the directories.
I tried to move the configuration into the web.config file, but the error still appear.
2016-07-26 09:17:25.6353 Warn Skipping unknown node: target
2016-07-26 09:17:25.6353 Trace ParseRulesElement
2016-07-26 09:17:25.6623 Error Error in Parsing Configuration File. Exception: NLog.NLogConfigurationException: Exception occurred when loading configuration from D:\Project\NLog.config ---> NLog.NLogConfigurationException: Target log not found.
w NLog.Config.XmlLoggingConfiguration.ParseLoggerElement(NLogXmlElement loggerElement, IList`1 rulesCollection)
w NLog.Config.XmlLoggingConfiguration.ParseRulesElement(NLogXmlElement rulesElement, IList`1 rulesCollection)
w NLog.Config.XmlLoggingConfiguration.ParseNLogElement(NLogXmlElement nlogElement, String filePath, Boolean autoReloadDefault)
w NLog.Config.XmlLoggingConfiguration.ParseTopLevel(NLogXmlElement content, String filePath, Boolean autoReloadDefault)
w NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
Structure of the project:
Update (after Michel A. answer): I have also properties in NLog.config file
Build Action = Content
Copy to Output Directory = Copy always
Update2: I found the solution. The problem was that I have the backslashes instead of forward slashes
Wrong syntax:
fileName="${basedir}\logs\${date:format=dd}.log"
Correct syntax:
fileName="${basedir}/logs/${date:format=dd}.log"
Update2: I found the solution. The problem was that I have the backslashes instead of forward slashes
I'm pretty sure that wasn't the problem. The paths aren't evaluated on registering the target. Also on Windows the kind of slashes don't matter.
The problem is here that the XML is lacking the <targets>:
<?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"
autoReload="true"
throwExceptions="true"
internalLogLevel="Trace"
internalLogFile="d:\temp\nlog-internal.log">
<target xsi:type="File" name="log" fileName="${basedir}\logs\${date:format=dd}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<rules>
<logger name="*" minlevel="Debug" writeTo="log" />
</rules>
</nlog>
It should be:
<?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"
autoReload="true"
throwExceptions="true"
internalLogLevel="Trace"
internalLogFile="d:\temp\nlog-internal.log">
<targets>
<target xsi:type="File" name="log" fileName="${basedir}\logs\${date:format=dd}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="log" />
</rules>
</nlog>
Hint: You get auto completion and error checking if you install the NLog.Schema package
Is your NLog config file being copied into your bin folder ?
(check property file in Visual Studio)
My NLog is configured as follows
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/log${shortdate}.txt" archiveAboveSize="500000" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="10" layout="${date:format=s}|${level}|${callsite}|${identity}|${message}|${exception:format=stacktrace}"/>
<!--<target name="console" xsi:type="Console" />-->
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
<!--<logger name="*" minlevel="Debug" writeTo="console" />-->
</rules>
</nlog>
But the stacktrace is not being logged when exception occurs. Is there a bug in NLog?
I have created a wrapper around Nlog and I am logging as follows
public void Error(string message, Exception ex)
{
logger.Error(message, ex);
}
I get the message in the log but not the stacktrace.
Thanks in advance
Try this in your layout:
${exception:format=ToString}
Have you tried using logger.ErrorException(message, ex); instead of logger.Error?