Enable Dynamic Logging for .Net Core Apps in PCF(i.e. TAS)

RamanaReddy V
5 min readApr 2, 2021

--

The 12 factor App methodology is a methodology for building software as a service application. Mostly we can use these principles whenever we implemented microservices on cloud-based applications. Here we can focus on Logs.

Logs provides a visibility into behavior of a running app. we can store the all microservices related logs into Centralized server so that we can easily debug the application issues. If we maintain specific log structures across all microservices it will help us to find out root cause of issues & we can easily integrate any log analytic tools.

In this article I’m focusing on how to enable Dynamic Logging Levels without application restart. In PROD environment apps we don’t enable Trace & Information related logs. If we enable that logs it’ll occupied high volume of disk space sometimes application may be down. Based PROD issue we can enable those logs with out application restart. This can possible in .net core related apps on PCF platform with help of Steeltoe lib’s.

Use Case:
If you want to find the root cause of PROD issues .net core deployed apps on PCF platform without application restart. If you want to set the dynamic logging levels based on issue with help of PCF platform & steeltoe lib’s.

Prerequisites:
1. PCF account (i.e. PCF part of VMware Tanzu https://login.run.pivotal.io/login It may differ )
2. CF CLI Installation (i.e. https://docs.run.pivotal.io/cf-cli/install-go-cli.html ) — Windows Platform choice.
3. .Net 5 with Blazor Framework (i.e. we can choose lower versions also)
4. steeltoe Packages from NuGet.
5. Create a .net core supported version of build packs with PCF.

Note: I’m using previous BlazorApp1 project for dynamic logging. for more details visit below articles.

  1. Service Registry pattern
  2. Config server

Example :

Install below NuGet packages into your project according your project need.

NuGet packages

Import the below namespaces in Program.cs file :

Namespace

Update the Program.cs file like below: Here logger.Write.Console()is optional. I can say not required.

Program.cs

Update the BlazorApp1 with basic Log levels for testing. we set out log level’s based on our requirement.

Ms1.cs

Update the Appsettings.Json like below based on your environment.

{"$schema": "https://steeltoe.io/schema/latest/schema.json","Serilog": {"MinimumLevel": {"Default": "Information","Override": {"Microsoft": "Warning","BlazorApp1": "Warning"}},"WriteTo": [{"Name": "Console","Args": {"outputTemplate": "[{Timestamp:HH:mm:ss}{Level:u3}]{SourceContext}:{Properties}{NewLine}{EventId}{Message:lj}{NewLine}{Exception}"}}],"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]},"Logging": {"IncludeScopes": false,"LogLevel": {"Default": "Warning"}},"Eureka": {"Client": {"EurekaServerServiceUrls": "http://localhost:8761/eureka/","ShouldRegisterWithEureka": false,"ShouldFetchRegistry": true}},"Management": {"Endpoints": {"Actuator": {"Exposure": {"Include": [ "*" ],"Exclude": [ "env", "refresh" ]}},"CloudFoundry": {}},"tracing": {"alwaysSample": true,"egressIgnorePattern": "/eureka/.*|/oauth/.*|/_blazor/.*","exporter": {"zipkin": {"endpoint": "https://localhost:5023/api/v2/spans"}}}},"APIContracts": {"MS1": "https://MICRO_SERVICE_1/","MS2": "https://MICRO_SERVICE_2/"}}

Update manifest.yml like below . we can add more configuration according to your need.

Manifest.yml

Publish the BlazorApp1 into PCF by using CF CLI . If you’re using CI/CD pipeline means we need to configure according to pipeline rules.

For now we can deploy through manual by using CF CLI.

a. Publish BlazorApp1 into any local directory (i.e. C:\Blazor\). Make sure manifest.yml should be in published location.
b. Login CF CLI & select corresponding Space with help of CMD.
c. After login into PCF navigate to published location within same CMD CF CLI login session (i.e. cd C:\Blazor\ )
d. After navigated into published location BlazorApp1 App then type cf push
e. You’re able to see deployment completed status logs of PCF in CMD.

Login into you’re PCF account you’re able see your deployed app within DEV space. Because of Steeltoe.Extensions.Logging.DynamicSerilogCore package we are able to see Steeltoe trace option & logo. We’ll get the Health check of application also etc…. features it will support.

Deployed App Overview page with steeltoe option
Health Check Option

In steeltoe Trace option we may get last app request information & how much time each request took place etc…. information available like below.

Request Trace Option

Open a deployed application & Navigate to Micro servce 1 link.

Deployed App

Go to Logs Section we’re able to see Configure Logging Level option & captured logs also like below

Logs Option

Click on Configure Logging Level option we’ll see log level options based on default log level option according to appsettings.json file.

Default Log Level
Log level Adjustments

Just switch the Log level WARN to FATAL like below with out application restart.

Logs Adjustment

Again Open a deployed application & Navigate to Micro servce 1 link. we’ll get the captured logs like below

Logs After adjustment

References :

  1. https://steeltoe.io
  2. https://docs.cloudfoundry.org/cf-cli/install-go-cli.html
  3. https://docs.cloudfoundry.org/devguide/deploy-apps/streaming-logs.html
  4. https://dotnet-cookbook.apps.pcfone.io/core/steeltoe-dynamic-logger/

Note:

I’m sharing this as per my knowledge. Anywhere, I did wrong. please let me know. I want to update myself.

=============== Happy Coding ===============

--

--

RamanaReddy V
RamanaReddy V

No responses yet