In my earlier post, we have discussed about using NLog in ASP.NET Core 3.1 Azure Web Application to write log files to Web App file system.
Azure Web App file system has limited storage, which is dependent upon the the app service plan and the number of instances you are running. Which means the logs files can easily grow and eat the space in the file system. To avoid this we can utilize the the following properties of file target which is available in NLog.
FileTarget.MaxArchiveDays – available from NLog 4.7 – Gets or sets the maximum days of archive files that should be kept. Following configuration will keep only last 30 days old log files and remove rest.
<target xsi:type="File" name="allfile" fileName="${aspnet-appbasepath}\logs\nlog-AspNetCore3-all-${shortdate}.log"
maxArchiveDays="30"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
FileTarget.MaxArchiveFiles – Gets or sets the maximum number of archive files that should be kept. Following configuration will keep only last 100 old log files and remove rest.
<target xsi:type="File" name="allfile" fileName="${aspnet-appbasepath}\logs\nlog-AspNetCore3-all-${shortdate}.log"
maxArchiveFiles="100"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
Now, we can try to add Microsoft.ApplicationInsights.NLogTarget, which will send the log files in an application insights resource for additional capabilities.
In order add this target we will first install the NuGet package in our project. This will install Microsoft.ApplicationInsights package as a dependency.
Next thing we need to do, is add a new target in our nlog.config file of type ApplicationInsightsTarget.
<target name="aiTarget" xsi:type="ApplicationInsightsTarget"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}">
<instrumentationKey>MY-APP-INSIGHTS-KEY</instrumentationKey>
<contextproperty name="threadid" layout="${threadid}" />
</target>
And add a rule to use this target.
<logger name="*" minlevel="Trace" writeTo="aiTarget" />
And don’t forget to add the extension to let nlog know where to find this new target
<extensions>
<add assembly="Microsoft.ApplicationInsights.NLogTarget"/>
</extensions>
As you can see from target, there is a node named instrumentationKey, which will hold the instrumentation key of your application insights resource. To get the key, open Azure Portal and go to your application insights resource and copy the instrumentation key and paste there.
Following is my full – updated 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"
autoReload="true"
throwConfigExceptions="true"
internalLogLevel="info"
internalLogFile="${aspnet-appbasepath}\logs\internal-nlog-AspNetCore3.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="Microsoft.ApplicationInsights.NLogTarget"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- File Target for all log messages with basic details -->
<target xsi:type="File" name="allfile" fileName="${aspnet-appbasepath}\logs\nlog-AspNetCore3-all-${shortdate}.log"
maxArchiveFiles="100"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="${aspnet-appbasepath}\logs\nlog-AspNetCore3-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|" />
<!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
<target xsi:type="Console" name="lifetimeConsole" layout="${level:truncate=4:lowercase=true}: ${logger}[0]${newline} ${message}${exception:format=tostring}" />
<target name="aiTarget" xsi:type="ApplicationInsightsTarget"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}">
<instrumentationKey>e******************c</instrumentationKey>
<contextproperty name="threadid" layout="${threadid}" />
</target>
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Output hosting lifetime messages to console target for faster startup detection -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<!-- BlackHole -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
<!-- Write to application insights -->
<logger name="*" minlevel="Trace" writeTo="aiTarget" />
</rules>
</nlog>
Okay, we are all set now. Lets run the project.
It might take few minutes for your logs to show in Application Insights resource in Azure, but take a look at the output window of you project. For the actions you want to log to application insights, there will be calls like the one highlighted in the following image.
And after few minutes of wait, data showed up in Azure Portal as well.
At this point we are able to add application insights target in our ASP.NET Core 3.1 App service web application and log data in Application Insights.
You must be logged in to post a comment.