Sending NLog logs to the server through WCF - c#

I have one server and many client applications, NLog is used everywhere. The purpose is to nearly completely remove logs from client and to send logs from a client directly to the server. It's successfully done through WCF, ILogReceiverServer and LogReceiverService target (as written there).
But the connection between server and client apps could be lost, so it's necessary to write Error messages to the file on the client side. And it should be written only when the connection is lost. I've studied NLog docs (should've studied better), but haven't found anything. Is it possible to check if logs were sent successfully or not? Or, may be, to enable/disable logger based on the result of logs sending?
Client config:
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="File" ... />
<target name="console" xsi:type="Console" ... />
<target xsi:type="LogReceiverService"
name="RemoteWcfLogger"
endpointConfigurationName="NetTcpBinding_ILogReceiverServer"
endpointAddress="net.tcp://address:port/LogReceiverServer"
useBinaryEncoding="True"
clientId=""
includeEventProperties="True">
</target>
</targets>
<rules>
<logger name="*" minLevel="Trace" writeTo="RemoteWcfLogger"/>
<logger name="*" minLevel="Error" writeTo="file" />
<logger name="*" minLevel="Trace" writeTo="console" />
</rules>
</nlog>

Maybe the Nlog FallbackGroup-target could be a solution:
https://github.com/nlog/NLog/wiki/FallbackGroup-target
So when the primary-target fails, then it will fallback to a secondary target.

Related

How to find out NLog files on Azure App service: Azure devops dashboard

I have configured NLog like so and all are working fine on development mode. I can see those files under this path: \bin\Debug\net6.0\logs.
I have published the app to the Azure App service. So can you tell me how to find out those files there?
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Trace"
internalLogFile=".\internal_logs\internallog.txt">
<targets>
<target name="logfile" xsi:type="File"
fileName=".\logs\${shortdate}_logfile.txt"
layout="${longdate} ${level:uppercase=true} ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>

Make NLog log even when testing with UnitTests c#

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

NLog works fine on windows .Net Core application but not on Linux

I've got this nlog.config file in my .Net Core console application:
<?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"
internalLogLevel="Error"
internalLogFile="c:\temp\IBTest\internal-nlog.txt">
<targets>
<target xsi:type="File" name="allfile" fileName="c:\temp\IBTest\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\IBTest\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="allfile" />
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
When I start my application on windows it works fine and creates all log files that should be created.
However, when I deploy my app on Linux it doesn't work.
This is how my nlog.config file looks on Linux:
<?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"
internalLogLevel="Info"
internalLogFile="/home/ib_tests_internal-nlog.log">
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="/home/ib_tests-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="/home/ib_tests_nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</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.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
I really don't get what's wrong. I checked the paths million of times, all files (at least the ones I think) have all rights to read/write.
I've tried dotnet publish and then dotnet app.dll in windows and it worked. When I start the application the same way on Linux it doesn't. And I'm really out of ideas. Seems that all I've been doing was copy-paste from samples I had. Does anyone have ideas what could be wrong? Or what else to check? or some manual way to create that log file and see if it runs? seriously been sitting half of the day to the point I thought I'm going to be fired
My guess would be the filename casing of your NLog.config is incorrect. Unless you have configured the code to use something other than the default, then nlog.config will not work, and it must be NLog.config (notice the capital N and L).
I just had a similar issue. Nlog worked fine on Windows and Mac development machines, but failed to log when deployed to Linux. The issue for me was permissions (I found this out by trial-and-error, not from any errors or messages). Try adjusting permissions on the directory that is being logged to.
You are logging directly to the /home directory, I would suggest updating you nlog.config to use a /home/logs subdirectory.
<target xsi:type="File" name="ownFile-web" fileName="/home/logs/ib_tests_nlog-own-${shortdate}.log" ...
Then set permissions with:
sudo chmod -R 777 /home/logs

NLog doesn't work in Azure

I'm trying to use NLog with an Azure Web App. Everything works on my local PC, but when I deploy to Azure it doesn't work at all.
I've tried with different targets like file and Azure table storage. Both work perfectly on my PC but not in Azure.
I'm using asp.net core.
Here is my NLog config for the table storage.
Note: this configuration works when I execute the program from my local PC. I can see the entries in the Azure table storage.
<?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"
>
<extensions>
<add assembly="NLog.Extensions.AzureTableStorage"/>
</extensions>
<targets>
<target xsi:type="AzureTableStorage" name="allfile"
PartitionKey="nlog-all-${date}.${logger}"
RowKey="${ticks}.${guid}"
ConnectionString="**REMOVED**"
tableName="**REMOVED**"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="AzureTableStorage" name="ownFile"
PartitionKey="nlog-own-${date}.${logger}"
RowKey="${ticks}.${guid}"
ConnectionString="**REMOVED**"
tableName="**REMOVED**"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile" />
</rules>
</nlog>
I noticed that the configuration file NLog.config was not getting updated (or is being overwritten) in Azure.
Renaming the configuration file to something different solved my problem.
However, I consider this a workaround only.
The NLog.config in Azure seems to be the one coming as example, and contains no targets at all.

How to integrate NLog to write log to Azure Streaming log

Currently I am using NLog to write my application errors to a text file.
How can I configure NLog to write the error messages to Azure Streaming Log apart from writing to a Azure Blob Storage?
the Azure Streaming Log captures what is sent to the Trace interface. If you configure NLog to send to that target, you can then easily access that through the output window in Visual Studio for instance.
Here is how I configured NLog.config to obtain this result:
<targets>
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring}" />
<target xsi:type="Trace" name="trace" layout="${logger} ${message} ${exception:format=tostring}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="f" />
<logger name="*" minlevel="Trace" writeTo="trace" />
</rules>
The first target should resemble the one you already have for logging to file, the second simply sends the data to the trace channel.
Hope this helps!

Categories