1、引用以下包
2、在Program.cs中修改如下:
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Error()
.MinimumLevel.Override("Microsoft", LogEventLevel.Error)
.WriteTo.File(Path.Combine("Logs", "log"), rollingInterval: RollingInterval.Day)
.CreateLogger();
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, config) =>
{
config.ClearProviders(); //Disabling default integrated logger
_environmentName = hostingContext.HostingEnvironment.EnvironmentName;
})
.UseStartup<Startup>()
.UseSerilog()
.Build();
3、在controller使用
public AuthController(ModelContext context,ILogger<AuthController> logger)
{
_context = context;
_logger = logger;
}
_logger.LogError(ex.Message);
注意:
如有使用 Authorize 验证,不要设置在controller上,即使token正确也会报401,需要设置在action上