Saturday, 10 August 2019

Date and Time in Console log in ASP.NET Core

One of the challenges that I found so far when moving towards ASP.NET Core is that the default console logger (built-in) that comes with it is not as verbose as I was expecting when displaying Dates and Times. To me this is the most obvious reason as I want to see when something happened:


As you can see in the image above, you can appreciate all the actions that are happening to the different controllers but there is no timestamp. This was raised as an issue with the aspnet team but it was deferred for later as it was not a critical piece of functionality...I guess who defines if this is critical or not. In any case, I'm the bearer of good news with the solution to your problems.

Serilog to the rescue!

With Serilog you can easily create a template so we display the message in any way we see fit. To accomplish this, here are the different components that you need (I'm already using the latest .NET Core 3.0 Preview 7):

Install the following packages:
  1. - Serilog.AspNetCore (latest, at the time of this example, was 2.1.2-dev-00028)
  2. - Serilog.Sinks.Console (latest, at the time of this example, was 3.1.2-dev-00779)
Add the following code to your Program.cs file:


And now you will see the log that you need:


Hope it helps!
Jordi

Sunday, 4 August 2019

Enable .NET Core 3.0 Preview 7 on Visual Studio 2019

I've started migrating all my ASP .NET Core 2.1 apps to the latest ASP .NET Core 3.0 but as it's still marked as a preview as of July 2019, you will have to tell Visual Studio to allow these kinds of packages. So for now, to create ASP.NET Core 3.0 projects with VS2019 (version 16.2.1), you should do the following:



  • Enable preview releases of .NET Core SDK
    • Click Tools | Options in the top menu
    • Expand Environment | .NET Core 
    • Ensure that "Use previews of the .NET Core SDK (requires restart)" checkbox is checked

Once you restart Visual Studio and install all the necessary components mentioned above, you should see the following option under Target framework in your project:


Now, to build your project and make it work is another thing, but at least the framework is there your you to play with!

Happy Coding!
Jordi