Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install serilog and configure in an asp .net 4.7.1 webapi

I can not find any resources for installing Serilog in an ASP.Net 4.7.1 WebApi project. Can someone help me out? There are a ton of .Net Core resources but that does not help.

like image 738
Jim Kiely Avatar asked Feb 22 '19 00:02

Jim Kiely


People also ask

Is Serilog compatible with .NET framework?

We recommend supported versions of each platform. For example, the current minimum version of . NET Framework supported by Microsoft is 4.6. 2, though we would recommend 4.8 or newer.

What is Serilog C#?

Serilog is a logging library for . NET and C# that allows for more detailed and structured logging than the default . NET logging library. Serilog can be used to log information about application events, errors, and performance metrics.


1 Answers

Install required NuGet packeges, open the Package Manager Console and type

Install-Package Serilog
Install-Package Serilog.Sinks.File

Create new static class with name logger that will have Serilog configuration

public static class Logger
{
    private static readonly ILogger _errorLogger;

    static Logger()
    {
        _errorLogger = new LoggerConfiguration()
            .WriteTo.File(HttpContext.Current.Server.MapPath("~/logs/log-.txt"), rollingInterval: RollingInterval.Day)
            .CreateLogger();
    }

    public static void LogError(string error)
    {
        _errorLogger.Error(error);
    }
}

Use logger class when you want to log error as below

Logger.LogError("Test error log!");
like image 172
ElasticCode Avatar answered Oct 19 '22 23:10

ElasticCode