Lets create our .Net Core console app project
>mkdir nlog_with_stackdriver >cd nlog_with_stackdriver >dotnet new consoleRun 'dotnet build' and 'dotnet run' to see if our new project builds and runs successfully.
Now we need to add NLog and Google.Cloud.Logging.NLog packages.
>dotnet add package nlog >dotnet add package Google.Cloud.Logging.NLog packagesCreate nlog.config file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<extensions> | |
<add assembly="Google.Cloud.Logging.NLog"/> | |
</extensions> | |
<!-- the targets to write to --> | |
<targets> | |
<target name="asyncConsole" xsi:type="AsyncWrapper"> | |
<target name="console" | |
xsi:type="ColoredConsole" | |
layout="${date} ${threadname:whenEmpty=${threadid}} ${level:uppercase=true} ${logger} ${message} ${onexception:${exception}}" /> | |
</target> | |
<target name="asyncStackDriver" xsi:type="AsyncWrapper"> | |
<target name="stackDriver" | |
xsi:type="GoogleStackdriver" | |
logId="Default" | |
layout="${longdate}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> | |
</target> | |
</targets> | |
<!-- rules to map from logger name to target --> | |
<rules> | |
<!--All logs--> | |
<logger name="*" minlevel="Trace" writeTo="asyncConsole" /> | |
<logger name="*" minlevel="Trace" writeTo="asyncStackDriver" /> | |
<logger name="Microsoft.*" maxLevel="Info" final="true" /> <!-- BlackHole without writeTo --> | |
</rules> | |
</nlog> |
In the project file include ItemGroup to copy nlog.config file to output directory during build and publish.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ItemGroup> | |
<None Include="nlog.config" CopyToOutputDirectory="Always" /> | |
<ItemGroup> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
Logger logger = LogManager.GetCurrentClassLogger(); | |
// Log some information. This log entry will be sent to Google Stackdriver Logging. | |
logger.Info("An exciting log entry for Google Cloud!"); | |
// Flush buffered log entries before program exit; then shutdown the logger before program exit. | |
LogManager.Flush(TimeSpan.FromSeconds(15)); | |
LogManager.Shutdown(); | |
} |
Now our sample app is competed and ready to deploy to Google Cloud Computer Engine VM. Looks like it should just magically work. We do not need provide any authentication with Google Cloud, nor we do not need to specify project ID in nlog.config. When running from within Compute Engine VM Google.Cloud.Logging.NLog will detect all needed settings and just run. But here is a catch...
We need to install Google Cloud Logging Agent on our Debian virtual machine.
>curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh >sudo bash install-logging-agent.sh
Thanks for sharing,
ReplyDeleteA small question, where do i place the GCP Stackdriver configuration
Hi!
ReplyDeleteThe configuration is placed in nlog.config file. My example above shows target name="stackDriver". Check it out the github repo as well.
Thanks.