NLog with AzureTableStorage - azure

Trying to insert logs into Azure table storage, while using the extension of NLog.Extensions.AzureTableStorage in Nlog.config file , i am getting error under target type for azure table storage.
Error : - This is invalid xsi:type http://www.nlog-project.org/schemas/NLog.xsd:AzureTableStorage
FYI - I am using latest version Nlog latest version 4.4.11 & i added extension of Nlog.extensions.azuretablestorage(Nuget version 1.1.4)
Update for config file:
<extensions>
<add assembly="NLog.Extensions.AzureTableStorage"/>
</extensions>
<!-- set up a an azure storage table target -->
<targets>
<target name="AzureTableStorage" xsi:type="AzureTableStorage" PartitionKey="${date}.${logger}" RowKey="${ticks}.${guid}" ConnectionString="UseDevelopmentStorage=true" tableName="TempAzureTableStorageTargetTestsLogs" />
</targets>

This is invalid xsi:type http://www.nlog-project.org/schemas/NLog.xsd:AzureTableStorage
I assumed that you have installed NLog.Config that would automatically create the default NLog.config. Since this package references NLog.Schema which would enable Intellisense(TM) when editing NLog configuration files.
I have checked with this issue, the above message is just for warning, the logging feature would be fine. And you could remove the NLog.Config and NLog.Config packages, then the warning message would go away.
FYI - I am using latest version Nlog latest version 4.4.11 & i added extension of Nlog.extensions.azuretablestorage(Nuget version 1.1.4)
AFAIK, the latest version for NLog Azure Table Storage Target is 1.0.11. Here is my code snippet, you could refer to it:
NLog.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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
<extensions>
<add assembly="NLog.Extensions.AzureTableStorage"/>
</extensions>
<targets>
<target xsi:type="AzureTableStorage"
name="NLogAzureTable"
ConnectionStringKey="NLog.Azure.TableStorage.ConnectionString"
TableName="NLogTable"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="NLogAzureTable" />
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>
App.config
<appSettings>
<add key="NLog.Azure.TableStorage.ConnectionString" value="{your-storage-account-connectionString}" />
</appSettings>
Usage:
var logger = LogManager.GetLogger(nameof(Program));
logger.Info("hello world!!!");
Result:
For more details about how to configure the NLog Azure Table Storage Target, you could refer to NLog.Extensions.AzureTableStorage. Also, you could refer to NLog for more tutorials.

Related

Sentry.NLog not logging to Sentry

Trying to get Sentry wired up via NLog, and not having much luck.
Packages:
<package id="Sentry" version="3.0.5" targetFramework="net462" />
<package id="Sentry.NLog" version="3.0.5" targetFramework="net462" />
Both latest.
NLog.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"
autoReload="true"
throwExceptions="true"
internalLogLevel="Debug"
internalLogFile="c:\temp\nlog-internal.txt"
internalLogToConsole="true"
throwConfigExceptions="true">
<extensions>
<add assembly="Sentry.NLog" />
</extensions>
<targets>
<target xsi:type="Sentry"
name="sentry"
dsn="https://secret"
environment="test"
includeEventProperties="True"
layout="${message}"
breadcrumbLayout="${message}"
minimumBreadcrumbLevel="Debug"
ignoreEventsWithNoException="False"
includeEventDataOnBreadcrumbs="False"
includeEventPropertiesAsTags="True"
minimumEventLevel="Error" />
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="sentry" />
</rules>
</nlog>
Things I've checked:
DSN. Copied and pasted from the Sentry portal, so i know it's correct
No filters on 'environments' in Sentry.
Nlog log file shows no errors. In fact in shows Sentry being wired up.
Added other targets to NLog (e.g console, file, etc), they work fine.
Any ideas?
Thanks
Did you opt out of InitializeSdk?
https://github.com/getsentry/sentry-dotnet/blob/6ce7f933f64bd517677b5e837d6c7e8cc37b4217/src/Sentry.NLog/SentryTarget.cs#L148-L155
Could that be the issue?
Unless you're using another integration which already initialized Sentry, of if you call SentrySdk.Init yourself though.
But by default the DSN alone should be enough to init the SDK.
You can set the SDK to debug mode to see what's happening.
There's a sample project in the repo:
https://github.com/getsentry/sentry-dotnet/tree/main/samples/Sentry.Samples.NLog
Sentry is sending data over the network, and propbably has an artificial delay to optimize for batching and to reduce network-traffic.
Did you remember to flush?, and wait for the data to arrive at destination.

Configuration NLog.config file for using on Azure portal

I've applied ASP.NET core 3.1 in my web application and I've used NLog for logging.
In local host I set the internalLogFile and fileName like this
internalLogFile="E:\Archives\Projects\Alpha\Alpha.Web.App\internal_logs\internallog.txt"
fileName="E:/Archives/Projects/Alpha/Alpha.Web.App/logs/${shortdate}_logfile.txt"
in nlog.config file. How can I set the paths for using in Azure?
<?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="Trace"
internalLogFile="E:\Archives\Projects\Alpha\Alpha.Web.App\internal_logs\internallog.txt">
<targets>
<target name="logfile" xsi:type="File"
fileName="E:/Archives/Projects/Alpha/Alpha.Web.App/logs/${shortdate}_logfile.txt"
layout="${longdate} ${level:uppercase=true} ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
nlog.config file is in this path:
https://{appname}.scm.azurewebsites.net/dev/wwwroot/nlog.config
I have used ${basedir} and changed values of internalLogFile and fileName to
internalLogFile="${basedir}/internal_logs/internallog.txt">
and
fileName="${basedir}/logs/${shortdate}_logfile.txt"
Now I access to these files in bin folder in my project.

Can layout render(s) be used for FileName(s)?

Can layout render(s) (processname to be exact) be used for FileName(s)?
Note,
the internalLogFile (and INTERNAL.log value)
and
fileName="${processname}.NLog.${shortdate}.PeanutButter.log"
values below.
<?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="${processname}.NLog.INTERNAL.log"
internalLogLevel="Trace" >
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="target1" fileName="${processname}.NLog.${shortdate}.PeanutButter.log"
layout="${date}|${level:uppercase=true}|${logger}|${environment-user:userName=true:domain=true}|****|${message} ${exception:format=toString,Data}|${all-event-properties}" />
<target xsi:type="Console" name="target2"
layout="${date}|${level:uppercase=true}|${logger}|${message} ${exception:format=toString,Data}|${all-event-properties}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="target1,target2" />
</rules>
</nlog>
With the above, I'm getting a file created:
${processname}.NLog.INTERNAL.log
(literally, that is the filename)
and no files at all named:
*PeanutButter.log
where * is a wild card search.
Imported packages below.
<ItemGroup>
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
</ItemGroup>
Can layout render(s) (processname to be exact) be used for FileName(s)?
Filetarget
Yes, it's fully supported for the file target.
See this simple example from the Filetarget docs:
Per-level log files
Single File target can be used to write to multiple files at once.
The following configuration will cause log entries for each log level
to be written to a separate file, so you will get:
Trace.log
Debug.log
Info.log
Warn.log
Error.log
Fatal.log
<target name="file" xsi:type="File"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}/${level}.log" />
Internal logger
${processname}.NLog.INTERNAL.log
(literally, that is the filename)
It's not supported for the internal logger filename!
From the Internalloger docs
internalLogFile
Note: only a few layouts are supported, as the internal log needs to be as stable as possible.
NLog 4.6+: Supported renderers (without options): ${currentdir}, ${basedir}, ${tempdir}
NLog 4.6+: Environment Variables are also supported: e.g. %appdata%
If the internal logger would fail because of (complicated) layout renderers, where should it log that then? ;)

NLog 4.0 ignores final attribute on a rule

My application is using NLog configured with NLog.config shown below. It also uses RavenDB database which by default uses active NLog settings for logging.
It produces a lot of DEBUG and INFO messages that pollute the log. I do want to log ERROR and WARN messages. All of the records created by RavenDB come from namespaces that start with Raven.
I created the rules as shown below. Basically there is a final rule that prevents INFO/DEBUG messages that come from Raven.* namespace to get written into the log file. Everything was working well until I upgraded the NuGet packages to NLog 4.0. Now all the RavenDB messages are written into the log file.
Is this a bug or there is some configuration change happened across the NLog versions?
<?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="file" xsi:type="AsyncWrapper" queueLimit="1000" overflowAction="Discard">
<target
name="file"
xsi:type="File"
layout="${longdate} - ${level:upperCase=true} - ${identity} - ${logger} - ${message}${onexception:inner=${newline}${exception:format=tostring}${newline}}"
fileName="D:/Logs/AppName/${shortdate}.log" />
</target>
</targets>
<rules>
<logger name="Raven.*" writeTo="file" minlevel="Warn" final="true" />
<logger name="*" writeTo="file" minlevel="Debug" />
</rules>
</nlog>
Apparently there was a logic change in NLog 4. It does not mark messages from Raven namespace with level below the Warn final any longer.
http://nlog-project.org/2015/06/09/nlog-4-has-been-released.html
So the rule would have to change to send messages in the Raven.* namespace with maxlevel="INFO" into a null target.

NLog Not Writing

I have added NLog to my project and in the development environment, it works fine.
I created a Setup file to deploy my application. The NLog.config file did not show up as a dependency in the Setup project. So, I added it as a file and it is present in the same directory as the exe file and App.config when deployed.
It does not do any logging. I don't know why. Here is the config file:
<?xml version="1.0" encoding="utf-8" ?>
<?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">
<variable name="day" value="${date:format=dd}" />
<variable name="month" value="${date:format=MM}" />
<variable name="year" value="${date:format=yyyy}" />
<variable name="verbose" value="${longdate} | ${level} | ${message} | ${exception:format=tostring,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}" />
<targets>
<target name="logfile" xsi:type="File" fileName="${basedir}/Logs/${year}${month}${day}.log" layout="${verbose}" />
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="logfile" />
</rules>
</nlog>
Any help would be great. Cheers!
Does NLog.config have the property "Copy to Output Directory" set as "Copy always"? https://stackoverflow.com/a/8881521/438760
Put your NLog configuration within the yourapp.exe.config file. Like so:
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog>
<variable name="day" value="${date:format=dd}" />
...
<targets>
<target name="logfile" xsi:type="File" .../>
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="logfile" />
</rules>
</nlog>
</configuration>
I'm guessing the double xml version statements (line 1 and 2) was a copy / paste issue....
Probably a dumb question, but you have the minLevel set to Error. Are you actually encountering errors that would be logged, or have you tried lowering this to info or debug?
For me, the reason my NLog stopped logging was different to the above suggestions.
During updating the packages, something must have automatically added an nlog section to the bottom of my Web.config. It may have been ApplicationInsights. Obviously this took preference and, this caused it to stop checking my Nlog.config file.
Once I removed the section from my web.config it started magically reading from my nlog.config file again. I took care to take the extentions, targets and rules from web.config and ensure they were placed neatly in my nlog.config file instead.

Resources