DEV Community

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

 
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.