问题
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