Nlog logs location overwritten by code still logging happening at config location - c#

NLog version - 4.4.3
Platform - .Net 4.5.2
Current NLog config -
<nlog autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="layout" value="${longdate}|${level:uppercase=true}|${threadid}|${logger}|${message}" />
<variable name="logLocation" value="logs" />
<targets async="true">
<target name="debugger" xsi:type="Debugger" layout="${layout}" />
<target name="console" xsi:type="Console" layout="${layout}" />
<target name="logfile"
xsi:type="File"
fileName="${logLocation}\${processname}.log"
archiveFileName="${logLocation}\\${processname}.{###}.log"
archiveEvery="Day"
archiveAboveSize="2048000"
archiveNumbering="Rolling"
maxArchiveFiles="10"
concurrentWrites="false"
keepFileOpen="false"
layout="${layout}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Debug" writeTo="debugger" />
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>
Code to override location
LogManager.ReconfigExistingLoggers();
var target = (FileTarget)LogManager.Configuration.FindTargetByName<AsyncTargetWrapper>("logfile").WrappedTarget;
target.FileName = $#"..\..\..\..\logs\Foobar.log";
What is the current result?
When application/service starts it writes to overwritten location, but sometimes (not sure of scenario - maybe rollover) it start writing to config location.
What is the expected result?
Logs should always be written to overwritten location.
Did you checked the Internal log?
No
Please post full exception details (message, stacktrace, inner exceptions)
No Exception
Are there any workarounds? yes/no
Restart service/application.
Is there a version in which it did work?
No idea. This is the version we started with and sticking to.
Can you help us by writing an unit test?
Unit tests won't help as it is an intermittent scenario.

You have auto reload (<nlog autoReload="true”) enabled, so if it needs to reload (after sleep or change in config), you will lose the changes made in code.
The solution it so disable autoreload, or set the change after reload again. See code example:
static void Main(string[] args)
{
UpdateNLogConfig();
LogManager.ConfigurationReloaded += LogManager_ConfigurationReloaded;
log.Info("Entering Application.");
Console.WriteLine("Press any key to exit ...");
Console.Read();
}
private static void LogManager_ConfigurationReloaded(object sender, LoggingConfigurationReloadedEventArgs e)
{
UpdateNLogConfig();
}
private static void UpdateNLogConfig()
{
//note: don't set LogManager.Configuration because that will overwrite the nlog.config settings
var target = (FileTarget)LogManager.Configuration.FindTargetByName<AsyncTargetWrapper>("logfile").WrappedTarget;
target.FileName = $#"..\..\..\..\logs\Foobar.log";
LogManager.ReconfigExistingLoggers();
}
See also Combine XML config with C# config · NLog/NLog Wiki

Instead of doing target lookup, and modifying target properties directly. Then I suggest making use of the NLog Layout Logic.
<nlog autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets async="true">
<target name="logfile"
xsi:type="File"
fileName="${gdc:item=logFile:whenEmpty=log/${processname}.log}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>
And then just assign the logLocation:
NLog.GlobalDiagnosticsContext.Set("logFile", $#"..\..\..\..\logs\Foobar.log");
Using GDC will also works very well with autoReload=true and no need to call LogManager.ReconfigExistingLoggers().

Related

NLog not sending to papertrail once deployed to an EC2

When deploying to my EC2 NLog no longer seems to be working correctly. When deployed locally, I have no issues logging the chat. On the other hand, it's rapidly writing to it's internal log and ignoring the value of if it should write to it's internal log at all.
I've tried switching the protocol type to be used from TCP to UDP, which stops it from writing errors (but also does not allow it to connect to PaperTrail which was the point). I've also tried turning off internal logging, which also does not work. Recopying over my NLog.config, as well as going through the initial tutorial multiple times. I've also checked my firewall settings for ports, the EC2 security group settings for ports, and the application permissions on the firewall.
NLog version: "4.6.2"
Platform:.Net 4.5
Current NLog config (xml or C#, if relevant)
<?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="false" internalLogFile="c:\temp\nlog-internal.log">
<extensions>
<add assembly="NLog.Targets.Syslog" />
</extensions>
<targets>
<target name="syslog" type="Syslog">
<messageCreation>
<facility>Local7</facility>
<rfc5424 hostname="${machinename}-Ragnarok" appName="ChatServer-Ragnarok" />
</messageCreation>
<messageSend>
<protocol>TCP</protocol>
<tcp>
<server>logs.papertrailapp.com</server>
<port>REDACTED</port>
<tls>
<enabled>true</enabled>
</tls>
</tcp>
</messageSend>
</target>
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minLevel="Trace" appendTo="syslog" />
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
The actual error is as follows (It's repeated every few seconds in the logs):
2019-04-15 13:35:22.3986 Warn SendAsync failed Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at NLog.Targets.Syslog.MessageSend.SocketInitializationForWindows.KeepAliveConfigurationIsUpToDate(KeepAliveConfig keepAliveConfig)
at NLog.Targets.Syslog.MessageSend.SocketInitializationForWindows.ApplyKeepAliveValues(Socket socket, KeepAliveConfig keepAliveConfig)
at NLog.Targets.Syslog.MessageSend.SocketInitialization.SetKeepAlive(Socket socket, KeepAliveConfig keepAliveConfig)
at NLog.Targets.Syslog.MessageSend.Tcp.Init()
at NLog.Targets.Syslog.MessageSend.MessageTransmitter.b__21_0(Task _)
at NLog.Targets.Syslog.Extensions.TaskExtensions.<>c__DisplayClass0_01.<Then>b__0(Task t)
at System.Threading.Tasks.ContinuationResultTaskFromTask1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
Does anyone have any ideas on how to get this to work / why it's not working when deployed?
Just a stopgap for now just to add the following to the config, which allows it to not crash on the "Keep Alive"
<?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="false" internalLogFile="c:\temp\nlog-internal.log">
<extensions>
<add assembly="NLog.Targets.Syslog" />
</extensions>
<targets>
<target name="syslog" type="Syslog">
<messageCreation>
<facility>Local7</facility>
<rfc5424 hostname="${machinename}-Ragnarok" appName="ChatServer-Ragnarok" />
</messageCreation>
<messageSend>
<protocol>TCP</protocol>
<tcp>
<server>logs.papertrailapp.com</server>
<port>REDACTED</port>
<tls>
<enabled>true</enabled>
</tls>
<keepAlive>
<enabled>false</enabled>
<time>15</time>
</keepAlive>
</tcp>
</messageSend>
</target>
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minLevel="Trace" appendTo="syslog" />
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
The code I've added in question is the following:
<keepAlive>
<enabled>false</enabled>
<time>15</time>
</keepAlive>
Edit:
A fix was just pushed to NLog.Targets.Syslog's git which fixed this issue for me entirely.
https://github.com/luigiberrettini/NLog.Targets.Syslog/pull/183

nlog settings for ASP .NET Core 2.1 site

I'm trying to setup nlog to log into 3 separate log files. I want Entity Framework stuff to go to one log file (db.log). I want application-related logging only to go to another log file (own.log). And finally I want everything else (Microsoft stuff) to go to yet another file (all.log)
This is what I have below, but I can't get logging to work based on my requirements. What am I doing wrong?
Thanks in advance!
<?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"
internalLogFile="internal.log"
internalLogLevel="info" >
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile"
fileName="all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web"
fileName="own-${shortdate}.log"
layout="${longdate}|${activityid}|${level:uppercase=true}|${message} ${exception:format=ToString}|${logger}|${all-event-properties}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="File" name="sqllogfile"
fileName="db-${shortdate}.log"
layout="${date}|${activityid}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.EntityFrameworkCore.*" minlevel="Trace" writeTo="sqllogfile" final="true" />
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<!-- Our log -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
Moving database log was the key. Also, after moving catch-all log to the bottom, I now get correct logging results.

NLog: Where is NLog Config?

I coded this:
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
public MyClass()
{
Console.Write("lol");
Logger.Debug("Debug test...");
Logger.Error("Debug test...");
Logger.Fatal("Debug test...");
Logger.Info("Debug test...");
Logger.Trace("Debug test...");
Logger.Warn("Debug test...");
}
And nothing displays.. so I was told to go and add <targets> to the config file, thing is.. where is the config file? Nothing on google, the documentation, or anything like that helps me...
From NLog Wiki:
The following locations will be searched when executing a stand-alone *.exe application:
standard application configuration file (usually applicationname.exe.config)
applicationname.exe.nlog in application’s directory
NLog.config in application’s directory
NLog.dll.nlog in a directory where NLog.dll is located (only if NLog isn't installed in the GAC)
So, the easiest would be to add a NLog.config file in the application’s directory
Sample Nlog.config File for your reference:
<?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"
throwConfigExceptions="true">
<targets>
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
See also: https://github.com/NLog/NLog/wiki/Tutorial
Alternatively you can define the config programmatically in C# as explained in the blog docs here:
https://github.com/NLog/NLog/wiki/Configure-from-code

Ignoring NLog Configuration referenced in other assembly

I have a unique problem with the NLog. I have a third-party assembly we use into our project. They have used the NLog into their project and referenced NLog configuration into their configuration which I don't have access to.
I initially had a problem even adding the NLog to a project since both version of dlls were different. I had to download the source code of NLog and change the assembly name since alias was not working for me (OR I didn't know how to use it.)
After giving my own assembly name, NLog started wotrking but.. It started logging third-party log information too with it. Looks like they were not careful enough when defining the logger and they just defined (*). Now my question is, how do I avoid their stuff from logging in to my file? I have also tried setting it to final = true.
Here is how my very simple configuration file looks like.
<?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">
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!-- add your targets here -->
<!--
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<target name="errorLog" xsi:type="File"
fileName="${basedir}/logs/error/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"
/>
<target name="generalLog" xsi:type="File"
fileName="C:/logs/${date:format=yyyy-MM-dd}.log"
layout="${longdate} ${uppercase:${level}} ${message}"/>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="Errors" writeTo="errorLog" minlevel="Warn"/>
<logger name="EBusiness.Model.*" writeTo="generalLog" levels="Trace,Debug,Info" final="true"/>
<!--
<logger name="*" minlevel="Trace" writeTo="f" />
-->
</rules>
</nlog>
If they are writing to the root logger, you should configure the root logger to not log to anywhere you care (maybe point it at a console logger) and your application can log to a specific logger which does write to your files.
Ugly, but should work.
For example:
Add this target:
<target name="console" xsi:type="Console" />
And Change the * rule to be:
<logger name="*" minlevel="Fatal " writeTo="console" />
Then, Add your own logger rule, say something like:
<logger name="MyNameSpace.*" minlevel="Trace" writeTo="logfile" final="true" />
Assuming you have a logfile target defined pointing at your file.

Do not allow users to delete log files

I have a simple app:
public class Program
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
while (true)
{
Logger.Info(DateTime.Now.ToString());
Thread.Sleep(5000);
}
}
}
And configuration:
<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="C:\Logs\log.txt" keepFileOpen="true" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile" />
</rules>
</nlog>
But NLog does not seem to lock log.txt (desipte keepFileOpen property being set to true) - I can delete it. Even worse - the log file is not recreated after it has been deleted. So if user accidentally deletes the file - there will be no logging until the application is restarted (or in more general case until new log file name takes place).
Is there any way to make NLog lock log files or at least recreate them after they have been deleted?
Use enableFileDelete setting to make NLog lock file:
<target name="logfile" xsi:type="File"
fileName="C:\Logs\log.txt"
keepFileOpen="true"
enableFileDelete="false" />

Categories