DEV Community

Cover image for What every ASP.NET Core Web API project needs - Part 1 - Serilog

What every ASP.NET Core Web API project needs - Part 1 - Serilog

Mohsen Esmailpour on February 27, 2021

In a series of articles, I'm going to show the implementation of an architecture that is suitable for a thin Web API project or Web API that fits i...
Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

Nice article.

There is one thing that you should be aware of when it comes to the Console sink. I’ve seen that you used it with the docker container.
Serilog’s Console the sink is synchronous and has no buffer. That means during the high load it slows down your application. The tread waits until the log is written. As a solution, there is an async sink nuget package. It introduces asynchronously logging and configurable buffers.
As a side note, other sinks like SQL, Application Insights are asynchronous by design.

Collapse
 
moesmp profile image
Mohsen Esmailpour

Yes, you right @rafal
I think using console sink with docker container it's not a good idea and doesn't make sense. I just wanted to make an example of how to override configuration. I'm going to change it with a proper example.

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

According to 12-factor-app it does have sense. I see your point of view. I just want to warn everyone:)

Collapse
 
jasonsbarr profile image
Jason Barr

This is great, but I can't find anything on how to set up and configure Serilog in ASP.NET 6, which completely revamps the Program class (and does away with having a separate Startup class). Do you know anything about how to do that? I really need logging in an API I'm setting up for work and I can't find any information at all on configuring Serilog with ASP.NET 6.

Collapse
 
moesmp profile image
Mohsen Esmailpour

I haven't tried .NET 6 and minimal APIs yet but this weekend I will try and let you know If I had any luck.

Collapse
 
jasonsbarr profile image
Jason Barr • Edited

I would appreciate that! Logging is the one significant piece my app needs that I can't figure out how to set up. It's a work project.

I know, I know, building a work project on a pre-release version of .NET probably wasn't the brightest idea, but since it's an LTS it made sense to me since it will be supported for a longer time than .NET 5.

The sticking point is I can't figure out where to put UseSerilog, or what else to use if that method isn't going to work.

Thread Thread
 
moesmp profile image
Mohsen Esmailpour • Edited

I did same thing with .NET Core RC1 :) It was very risky, but we did the project successfully.
This code works for me and try it

using Serilog;

IHostBuilder host = builder.Host
    .ConfigureDefaults(args)
    .UseSerilog((hostingContext, loggerConfiguration) =>
                    loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
                        .Enrich.FromLogContext());
Enter fullscreen mode Exit fullscreen mode

Code

dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
jasonsbarr profile image
Jason Barr

Well, it doesn't generate an error or crash, so that's good. When I run dotnet run it hangs, but when I run dotnet build it runs without a hitch.

Sorry for being such a n00b but this is literally my first .NET project so everything is new and uncertain.

Thread Thread
 
jasonsbarr profile image
Jason Barr

I figured it out - I forgot to tell it in appsettings.Development.json where to output the log info. Silly beginner mistake.

Thread Thread
 
jasonsbarr profile image
Jason Barr

Now how do I actually access the logger and use it? Do I need to create an instance somewhere or set it up for dependency injection?

Thread Thread
 
moesmp profile image
Mohsen Esmailpour

No, just inject I ILogger into classes you want to log data.

Thread Thread
 
jasonsbarr profile image
Jason Barr

Got it working! Thanks for taking the time to help me out.

Collapse
 
alexisincogito profile image
Alexis Incogito • Edited

That's exactly what I was looking for, thank you for putting this project together.
I already understand all of the pieces, but being unfamiliar with ASP.NET I was missing a boilerplate template that save me the time of piecing everything together, one blog/documentation at a time.

Great idea!

Collapse
 
marcosbelorio profile image
Marcos Belorio

Nice post, thanks for sharing, it's simple and full of content

Collapse
 
rmaurodev profile image
Ricardo

Me too. Very nice article. Thanks for sharing.

Collapse
 
uthmanrahimi profile image
Uthman Rahimi

Looking forward to reading the rest of this series.