Install serilog and configure in an asp .net 4.7.1 webapi

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 06:50:57

问题


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.


回答1:


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!");


来源:https://stackoverflow.com/questions/54818504/install-serilog-and-configure-in-an-asp-net-4-7-1-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!