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:


Thursday, 4 July 2019

Code format and beautifier for code snippets in blogger.com

In almost all my blog posts I show code snippets or command line examples. To make code examples more readable they need to have code highlighting. One very nice and easy option that I liked for code highlighting is to use github gists. On my github I created one multi-file gist. To include only one file from multi-file gist use code sample below. Replace name before js with one provided by gist’s tag. Replace FileName with name of the file in the gist.

The sample of commands I like to show in the sample box that is not prettifying the text. Use the sample code below to wrap the sample command between “pre” tags.

Example of the sample command box.
C:\run_me.exe