Tuesday, March 24, 2020

Integrating Serilog to ASP.NET Core, Log to a rolling file and Removing the log noise

In this example I am using .NET Core 3.1 version which has LTS from Microsoft.

1) Installing Serilog

 Get Serilog to your solution using the Nuget package "Serilog.AspNetCore".























2) Removing the default logging

In the appsettingss.json you will get a configuration setting section for the built in logging which gets created for each ASP.NET Core project. Since we are not using it anymore we can remove that section.

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }

3) Adding Serilog to the solution

In the program.cs file set the Serilog configurations

public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
   .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
   .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
   .Enrich.FromLogContext()
   .WriteTo.File("C:\\ProgramData\\MyWebApplication\\MyLog.txt",
  rollOnFileSizeLimit: true,
  fileSizeLimitBytes: 2000,
  retainedFileCountLimit: 5)
   .CreateLogger();
try
{
Log.Information("Starting up");
CreateHostBuilder(args).Build().Run();

}
catch (Exception ex)
{
Log.Fatal(ex, "Application start-up failed");
}
finally
{
Log.CloseAndFlush();
}
}

You may have to use the following using directives:

using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact;

Now, specify that you will be using serilog when the HostBuilder is created.

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

4) Add logger to your controller class

Example:

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MySampleController : ControllerBase
    {
        private readonly ILogger<MySampleController> _logger;

        //constructor
        public MySampleController (
            ILogger<MySampleController> logger)
        {
            _logger = logger;
        }

     //sample Action
        [HttpPost]
        public async Task<ActionResult<Payload>> PostEmployeeInfo([FromBody]JObject notification)
        {
           try
          {
                _logger.LogInformation("Post Employee Info Success for :{0} {1}", paramOne, paramTwo);
           }
          catch(Exception ex)
         {
               _logger.LogError("Error - Post Employee: Message: {0} StackTrace: {1}", ex.Message, ex.StackTrace);
          }
        }
    }

How is the rolling file implemented ?

In the above code we have already specified it in the Log configuration section. See below code block:

.WriteTo.File("C:\\ProgramData\\MyWebApplication\\MyLog.txt",
  rollOnFileSizeLimit: true,
  fileSizeLimitBytes: 2000,
  retainedFileCountLimit: 5) 

This states that we are rolling the files based on a maximum file size limit. In this example I have specified the size to be only 2KB, But in a practical scenario a 5MB file size would be fine. I have set the number of rolling files to 5. So there will be a maximum of 5 files at a given time. The file containing the oldest logs will be removed when a new file is created.

How is the Noise removed ?

Serilog has many log levels:

Level
Usage
Verbose
Verbose is the noisiest level, rarely (if ever) enabled for a production app.
Debug
Debug is used for internal system events that are not necessarily observable from the outside, but useful when determining how something happened.
Information
Information events describe things happening in the system that correspond to its responsibilities and functions. Generally these are the observable actions the system can perform.
Warning
When service is degraded, endangered, or may be behaving outside of its expected parameters, Warning level events are used.
Error
When functionality is unavailable or expectations broken, an Error event is used.
Fatal
The most critical level, Fatal events demand immediate attention.
[Reference: GitHub Serilog Configuration Basics]

Normally you would set the logs minimum level to Information.

Example:
Log.Logger = new LoggerConfiguration()
       .MinimumLevel.Information()

There is a catch here. Serilog starts to log all Information level logs. It logs information generated related to AspNetCore. I also have the Entity Framework Core in the solution. So now I also get Information level logs generated regarding EntityFrameworkCore. A sample log would look like:



But we don't need all this noise in he log all the time. So we have to specify it when we configure the logging. The following lines overrides the default settings.

   .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
   .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)

It sets the logs generated regarding AspNetCore  and EntityFrameworkCore to a higher level which is "Warning". So you would get only the warning and Fatal information from these two components. Now the log will be clearer and you can easily spot the information logs which you place in the Controller using the _logger.LogInformation() method.

Resulting file structure:
MyLog_003.txt
MyLog_004.txt
MyLog._005txt
MyLog_006.txt
MyLog_007.txt

Note that the initial log created will be like "MyLog.txt". Then increment as MyLog_001.txt. As the log files are rolling it will increase the file numbering and remove the older files.

Saturday, March 14, 2020

ASP.NET Core - The requested page cannot be accessed because the related configuration data for the page is invalid

Scenario:

You have installed the .NET Core SDK in your computer. Debugs in the Visual Studio are working fine. Then you publish the solution to a folder location in IIS. Then you get the below error:

Error:

The requested page cannot be accessed because the related configuration data for the page is invalid 

HTTP Internal Server Error 500.19

Error code 0x8007000d

Fix:

Install the Windows hosting bundle from this link: https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.1-windows-hosting-bundle-installer

[Note: The version 3.1.1 is current as of this writing]

Installation:



VoilĂ  !! Your ASP.NET Core application starts to work in IIS

Saturday, March 7, 2020

Create a simple Login using CookieAuthentication in ASP.NET Core

I am using Core 3.1

1) Configure Services in the Startup.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o => o.LoginPath = new PathString("/Home/Login"));
}

2) Add Configuration in the Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseAuthentication();
}

3) Create a LogIn action method in the Controller

Here the user login information is received through a ViewModel

[HttpPost]
public ActionResult Login(LoginViewModel user)
{
if (ModelState.IsValid)
{
if (!string.IsNullOrEmpty(user.UserName) && !string.IsNullOrEmpty(user.Password))
{
var userId = _configuration.administrator.UserName;
var userPassword = _configuration.administrator.Password;

if (user.UserName == userId && user.Password == userPassword)
{
var claims = new[] { new Claim(ClaimTypes.Name, user.UserName),
new Claim(ClaimTypes.Role, "Administrator") };

var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity));

return RedirectToAction("Index", "MyBusinessController");
}
else
{
TempData["LogInMessage"] = "LogIn failed. Incorrect username or password";
return RedirectToAction("Login");
}
}
}
return View(user);
}

4) Display the Logged in user probably in the _Layout.cshtml

                    @if (Context.User.Identities.Any(i => i.IsAuthenticated))
                    {
                        <div class="nnav navbar-nav" style="padding:10px 15px;float: right;text-align: left;color:#9d9d9d;padding-top: 15px;">
                        Welcome @Context.User.Identity.Name
                                    @Html.ActionLink("(Logout)", "Logout", "Home")
                        </div>
                    }

5) Logout action method
[Authorize]
public ActionResult Logout()
{
HttpContext.SignOutAsync(
CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToAction("Login");
}

Sunday, March 1, 2020

Adding swagger to your ASP.NET Core Project

1) Installation
Install the Nuget Package "Swashbuckle.AspNetCore" to your project.







2) Add swagger services to middleware

Add the following code in the startup.cs classes ConfigureServices method

public void ConfigureServices(IServiceCollection services)
       {
services.AddSwaggerGen( c => {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
                }

3) Enable swagger

In the configure method add the below code

            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
           {
                        //Enable to serve swagger as a JSON end point
app.UseSwagger();
                        //Enable to serve the swagger UI
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
            }

4) Configure the Debug launch
In the project properties, Debug section under the "Launch browser" add the URL
Now when you start debugging the browser will open up the swagger for you.