Showing posts with label NLOG. Show all posts
Showing posts with label NLOG. Show all posts

Friday, 19 July 2019

Sample .Net Core Service Hosting

Windows Service, Linux daemon, Google Cloud Compute Engine

Today I am going to look at implementing .Net Core Generic Service Host and host it in .Net Core app to run as daemon under Linux. We will use Google.Cloud.Logging.NLog library to integrate NLog with Stackdriver. See "Integrating NLog with Stackdriver for error reporting on Google Cloud" for more details. App will be deployed on Google Cloud Compute Engine VM running Linux and will run as daemon service. In this sample service I will run a thread that will post an information message to Google Logging Interface. The sample code for this project is hosted on github.

We will create sample_service_hosting .Net core console project, which will implement our SampleService that will post messages to Google Logging Interface.

1. Create sample_service_hosting .Net Core console project

>mkdir sample_service_hosting
>cd sample_service_hosting
>dotnet new console
Add the following package references

NLog, NLog.Extensions.Hosting and Google.Cloud.Logging.NLog are also added for a final stage of the project to support logging messages to Google Cloud Log Viewer.

2. Implement HostBuilder configuration


Now we are going to implement HostBuilder, which will run our Generic Host with RunAsync call. I normally create it in the separate CreateHostBuilder function. It will return null if for some reason we failed to create our generic host and in Main we will need to check for returned result.
You notices that we adding appsettings.json file as well. Lets add it to our project.And a SampleService class, which we will extend in the next section. Add SampleService.cs under Services folder.

3. Implement SampleService hosted service


In this section we are going to inject nlog into our generic host builder and extend our SampleService host to log message every 10 seconds. We already configured HostBuilder to inject logger in ConfigureLogging section. We need to add .UseNLog() call just after var builder = new HostBuilder() in CreateHostBuilder function. In the SampleService class we will use CancellationTokenSource to cancel our service and stop Run thread when IsCancellationRequested is true. Run function will be called asynchronously and pointer to Task object is saved.
Logger is logging Trace messages and information messages within iteration loop. Add nlog.config to project.

4. Enabling gradual stop of the service on SIGTERM signal


We can deploy this sample_service_hosting app to Linux VM and make it run as a daemon, but with its current implementation it will not properly respond to SIGTERM sent from Linux host to gradually stop and clean up used resources. For this we are going to use injected IApplicationLifetime object and register events ApplicationStarted, ApplicationStopping and ApplicationStoped.

5. NLog.config - Add support for Stackdriver logging


At the start we already added reference to Google.Cloud.Logging.NLog package. Now we need to update nlog.config file to include assembly Google.Cloud.Logging.NLog and GoogleStackdriver target to log to Google Logging Interface.

Conclusion


Our sample_service_hosting app is done. Check the final version of this project on github. I am not going to cover here setup of this app as a daemon on Linux VM Google Cloud Compute Engine. This will be a topic of separate blog post. Stay tuned. 

References:



Wednesday, 10 July 2019

Integrating NLog with Stackdriver for error reporting on Google Cloud

In this post I will create a .net core console app, which will be hosted on Google Cloud Compute Engine VM running Debian Linux console only OS. For this we are going to use Google.Cloud.Logging.NLog .NET client library to integrate Google Stackdriver Logging with NLog. The sample code for this app is hosted on github repository.

Lets create our .Net Core console app project
>mkdir nlog_with_stackdriver
>cd nlog_with_stackdriver
>dotnet new console
Run '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 packages
Create nlog.config file

Important to note the use of assembly Google.Cloud.Logging.NLog and in asyncStackDriver target I used logId as "Default". LogId can be set to be the name of the console app or the project.

In the project file include ItemGroup to copy nlog.config file to output directory during build and publish.

Add the following code to Main.

At the end of the program before closing it is important to Flush resources and Shutdown the logger. Windows 10 gives limited time for application to close and under linux as well as Windows important to clear and close external resources used. In our case we are using Google Cloud Logging Agent.

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

References:


Wednesday, 2 January 2019

.Net Core console app with NLOG

In this post I am going to create simple minimalistic dotnet core console application and integrate it with nlog.

Let's create a folder for our project. I am going to name it nlog_console.

$mkdir nlog_console
$cd nlog_console

Now we create dotnet core console app project and if you have Visual Studio Code installed run the second command below.

$dotnet new console
$code . &

Now lets add nlog package to a project.

$dotnet add package nlog

This will add the following code to our project file.


Nlog framework requires configuration file 'nlog.config' to specify logger tagerts and rules. We can copy paste the sample nlog.config file from this github page or use one below.


'nlog.config' file need to be copied to destination directory during project build. To achieve it add the code below to a project file.


Now we need to add actual logging code to a program. Copy paste the Program class code below.


To build and run our console app run code below or add launch.json config to Visual Studio Code.

$dotnet build
$dotnet run

You should see the following output and log file created in the same directory as our console app.

2018/06/19 18:48:13.130|TRACE|Loggin trace. |nlog_console.Program|

References: