I would like to modify my NLog configuration, but I have the impression that my modifications are not taken into account.
According to this URL, the NLog configuration can be found:
In the file applicationname.exe.config. In my case, this file only contains the following NLog related lines:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Modules" />
<dependentAssembly>
<assemblyIdentity name="NLog" publicKeyToken="..." culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
applicationname.exe.nlog in application’s directory (there is no such file).
NLog.config in application’s directory. This file exists and contains the following entries:
<?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">
<targets>
<target name="logfile" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
<target xsi:type="File" layout="${longdate} | ${threadid} | ${logger} | ${message} ..." fileName="${basedir}/Logs/${shortdate}.tralala.${level}.log" />
</target>
<target name="console" xsi:type="Console" />
</targets>
The way the logger is loaded in the application is as follows:
using NLog;
...
private static readonly Logger log = LogManager.GetCurrentClassLogger();
My logfiles are always of the form "${basedir}/Logs/${shortdate}.log" instead of the mentioned "${basedir}/Logs/${shortdate}.tralala.${level}.log", so I have the impression my NLog.config file is not taken into account (or I'm working with the wrong target).
Which one is it and how can I work in order to be able to take my configuration into account?
Oh, for your information, I'm working on a Windows-10 computer, this is the name of my NLog.config file as shown in my Linux subsystem (in order to ensure the case sensitivity):
WSL Linux Prompt> ls -ltra | grep "NLog.config"
-rwxrwxrwx 1 user group 742 Mar 3 13:22 NLog.config
Edit:
By the way: is there a way to configure NLog in order to start every logging session with some information about the used configuration?
Edit: internal log results:
When I enable internal logging, this is what I get:
2022-03-17 10:25:52.8775 Debug Targets for Test by level:
2022-03-17 10:25:52.8775 Debug Trace =>
2022-03-17 10:25:52.8775 Debug Debug => Console LogFile Other_Application
2022-03-17 10:25:52.8775 Debug Info => Console LogFile Other_Application
2022-03-17 10:25:52.8775 Debug Warn => Console LogFile Other_Application
2022-03-17 10:25:52.8775 Debug Error => Console LogFile Other_Application
2022-03-17 10:25:52.8775 Debug Fatal => Console LogFile Other_Application
As you can see, no filePath or so, I have no idea where NLog gets its configuration from.
As the title says, I want my Logger to not be ignored when running unit tests using XUnit. I want it still to log. If this is possible, how can it be done?
Here is my config
<?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">
<targets>
<target name="log"
xsi:type="File"
fileName="${basedir}/logs/log.${longdate:cached=true}.log"
layout="${message}"
archiveFileName="${basedir}/logs/archives/log.${shortdate}.{#}.log"
archiveAboveSize="5242880"
archiveEvery="Day"
archiveNumbering = "Rolling"
maxArchiveFiles="20"
/>
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="trace" writeTo="log" />
</rules>
</nlog>
I just have a backend and no frontend, so when I run the tests I want to see that everything logs :)
You could use NLog in unit tests. Unfortunately it's difficult for NLog to find the path to the nlog.config as unit test frameworks move the binaries between folders - and not the nlog.config.
Also it's different in different environments/frameworks (NUnit, xUnit, MSTest, .NET full, .NET Core)
You could do:
Write the configuration in C#. See Docs
Or tell NLog the path to the config:
LogManager.Configuration = new XmlLoggingConfiguration("pathToNLogConfig/nlog.config");
Also recommend for logging in unit tests, write to the memory target instead of file/database etc. You could also retrieve in code the logged events. See memory target docs
I have a solution I built using .NET 4.7. This solution has two projects:
A class library (.NET 4.7.2)
A Unit Test Project (.NET Framework)
I'm trying to migrate this solution to use .NET Standard | Core.
I have successfully transferred the class library to a .NET Standard 2.0 project. I have also transferred the Unit Test Project to a .NET Core 2.0 mstest project. Everything compiles. I can run my tests as expected. However, no logs are getting written via NLog.
In my .NET 4.7 version of the solution, when I ran the unit tests, an actual log file was written via NLog. However, in the new .NET Standard | Core implementation, this log file is not getting written. I have the following two files in my mstest project:
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="c:\temp\console-example-internal.log"
internalLogLevel="Info" >
<targets>
<target name="myLogs" xsi:type="File" deleteOldFileOnStartup="true" fileName="${basedir}/logs/MyLogs.json" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${message}" />
<target name="traceLogs" xsi:type="File" deleteOldFileOnStartup="true" fileName="${basedir}/logs/trace.log" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="[${longdate}] ${message}${exception:format=ToString}"></target>
</targets>
<rules>
<logger name="MyLogger" minlevel="Info" writeTo="myLogs" />
<logger name="TraceLogger" minlevel="Trace" writeTo="traceLogs" />
</rules>
</nlog>
In my mstest project, I've also added references to:
Microsoft.Extensions.DependencyInjection
NLog
NLog.Extensions.Logging
I followed the steps outlined here. Except, I didn't do anything beyond step 2. Do I need that stuff for a mstest project? If so, where? It seems like a lot of additional stuff for something that was already working.
When running unit tests, it's hard to find the nlog.config as implementations will move the DLLs (and most of the time not the nlog.config) to temporary folders.
It's recommend to configure NLog from code in an unit test project. (how-to here)
Another option is to load the NLog config file manually. You need to provide the path to nlog.config to the XmlLoggingConfiguration contructor:
LogManager.Configuration = new XmlLoggingConfiguration(pathToNLogConfig);
Off topic but related:
In the unit test it's recommend to write the eventlogs not to a file, but in memory, e.g. the Memory target. It makes the unit test more robust.
I am using ServiceStack framework and NLog to do my logging. I can do logging on my local machine just fine. However on the server, it doesn't. I have checked that the Nlog.config is in the bin directory and the whole directory, including the directory above the bin directory has write access.
Below is a snippet of the config file:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets async="true">
<target xsi:type="File" name="file" fileName="${basedir}\logs\${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" />
<target xsi:type="Debugger" name="debug"
header="===== ${date:format=ddd, dd MMM yyyy} ${time} ====="
layout="${level} | ${logger} | ${message} | ${onexception:${exception:format=tostring} | ${stacktrace}}"
footer="===== end of session ===== ${newline}"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file,debug" />
</rules>
</nlog>
What might be the problem?
Have you tried to change the path to something you know?
Instead of fileName="${basedir}\logs\${shortdate}.log" to something like fileName="c:\logs\${shortdate}.log"?
The basedir variable of NLog in web applications don't run inside the bin folder. I think you will find the log files inside
%SystemRoot%\Microsoft.NET\Framework\versionNumber\Temporary ASP.NET Files
If it's a ASP.NET app.
While I do not know specifically what is wrong with your server environment and config I have always found it helpful to check the following:
Make sure you know exactly what the directory for the logs is (as suggested by #PauloCorreia).
Make sure that directory actually exists.
Make sure that the IIS worker process has the correct privileges to be able to write to that directory.
FWIW, it is usually a permissions issue in my experience.
I am trying to add logging to an application running on mobile device with Windows Mobile 6.1. � .NET Compact framework 3.5. using NLog.
I have the appropriate version of the NLog distribution installed.
However no log files are being created.
Here is my NLog.config file:
<?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">
<targets>
<target name="logfile" xsi:type="File" fileName=".\Neolant.ASRM.Terminal.log" layout="${longdate}|${level}|${message}|${exception}" autoFlush="true"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>
And here is the test code I was using:
public static void Main()
{
try
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.Info("test test test.");
try
{
throw new Exception("Unexpected!");
}
catch (Exception e)
{
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.WarnException("An exception occured.", e);
}
throw new Exception("Suddenly!");
}
finally
{
NLog.LogManager.Flush();
}
}
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var logger = NLog.LogManager.GetLogger("UpperLevel");
logger.FatalException("Application closed due to exception.", unhandledExceptionEventArgs.ExceptionObject as Exception);
NLog.LogManager.Flush();
}
I had this problem turned out that my log file was not being copied to my build directory. The NLog github page had the answer. (I've reformatted the paragraph a little for better readability.)
https://github.com/NLog/NLog/wiki/Logging-troubleshooting
NLog cannot find the configuration file. This can happen when the NLog.config file is configured with Build Action = None or Copy to Output Directory = Do not copy in Visual Studio.
Set Build Action = Content and "Copy to Output Directory = Copy if newer to fix this)
The log file was being created - but not in the application directory.
Using ${basedir} layout renderer as part of the file name proved to be a solution.
from nlog troubleshooting guide
Please check Nlog.config file properties: Copy to output directory should be Copy always
Please view image link https://i.stack.imgur.com/AlUG5.png
In case the response marked as answer is not all that clear you can check the example
<targets>
<target xsi:type="Console" name="console"
layout="${longdate}|${level}|${message}" />
<target xsi:type="File" name="ErrorLog" fileName="${basedir}/error.txt"
layout="${longdate}
Trace: ${stacktrace}
${message}" />
<target xsi:type="File" name="AccessLog" fileName="${basedir}/access.txt"
layout="${shortdate} | ${message}" />
</targets>
Taken from here using AppData location in NLog
My issue was permission related, the log file needs to allow the process to write to it, without write permissions you'll get no file.
Here's how to fix it for websites in IIS:
Right click on your folder in windows explorer and select properties
Choose the security tab
Click edit
Click add
In the textbox type 'IIS AppPool\YourAppPoolName' replace YourAppPoolName with the actual name of the application pool your site runs under
Click Check names
Click OK
Security footnote:
From a security aspect the best practice is to use ApplicationPoolIdentity as it is a dynamically created, unprivileged account.
more reading here: https://learn.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities
From the nlog troubleshooting guide:
https://github.com/NLog/NLog/wiki/Logging-troubleshooting
If you know that your config file is definitely being found, temporarily replace the section of your NLog.config file with the following and try it.
This rule will match any logger you've created, and if it works, it will put a log.txt file in the 'base directory' - which is the 'bin' directory for your test instance e.g. if you're running in debug mode, you'll see log.txt in your bin > debug folder. (This isn't explained very clearly in the troubleshooting guide).
If this works then you know that the problem is with your rules:
<nlog throwExceptions="true">
<targets>
<target name="file" type="File" fileName="${basedir}/log.txt" />
</targets>
<rules>
<logger name="*" minLevel="Trace" writeTo="file" />
</rules>
</nlog>
I found that only name="file" worked for the target - other values didn't
Adding the throwExceptions="true" as above will also ensure that you get useful error messages when you're debugging.
In my case, I've missed the rules after defining the rules works like a charm
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile" />
<!-- 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>
Spent a lot of time on this issue. This was my problem. I was using a Setup project to install a Windows Service with an MSI. I had to manually add NLog.config to the output of the installer to make sure it got copied to the install directory of the service
for simple troubleshooting purposes, launch VisualStudio to run as administrator. This will help to sort out permissions to create log files while debugging.
Also use createDirs=true in each of the target section to automatically create missing folders in the file path provided in target section.
I also faced the same issue, finally i have solved it. I had a web application, where i want to implement NLog. Please find the following steps to implement NLog.
Step 1:- Go to NuGet packages manager and install following packages.
Step 2:- Open Web.config and add those following line
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<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="Off" internalLogFile="D:\projects\NlogWeb\nlog-internal.log">
<targets>
<target name="console" xsi:type="ColoredConsole" layout="${message}" />
<!--Write logs to File-->
<target name="file" xsi:type="File" fileName="D:\projects\NlogWeb\ErrorLogFile.log" layout="--------------------- ${level}(${longdate})${machinename}-------------------- ${newline}
Exception Type:${exception:format=Type}${newline}
Exception Message:${exception:format=Message}${newline}
Stack Trace:${exception:format=Stack Trace}${newline}
Additional Info:${message}${newline}" >
</target>
</targets>
<rules>
<logger name="*" minlevel="trace" writeTo="file" />
</rules>
</nlog>
Step 3:- Now the last configuration to call in your .cs file.
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace NlogWeb
{
public partial class Home : System.Web.UI.Page
{
private static Logger logger = LogManager.GetCurrentClassLogger();
protected void Page_Load(object sender, EventArgs e)
{
try
{
throw new Exception("Divide By Zero Exception", new DivideByZeroException());
}
catch (Exception ex)
{
logger.Error(ex.Message);
}
}
}
}
We have done. Now execute your code and enjoy logging.
To fix this, I had to run my application in administrator mode. I suspect windows had an update that suddenly prevented exe's from creating a (log) file, since the exe could always prevously log without admin rights.
In my case I had to load the NLog.config file manually in the code since it wasn't found automatically. Loading the configuration must be done before logs are generated.
LogManager.LoadConfiguration(#"D:\doe\ConsoleApp2\ConsoleApp2\NLog.config");
After that I got log files and console output.
in my case, a WebAPI application, I solved the problem by giving modify permissions to IIS_IUSRS the modify permission for the website folder C:\inetpub\wwwroot\my_website