Akh*_*hil 1 c# asp.net-web-api2 serilog
我需要将 seri 记录器的自我日志启用到文本文件。我的配置如下;
__serilogLogger = new LoggerConfiguration()
.Enrich.WithProperty("ApplicationIPv4", _ipv4)
.Enrich.WithProperty("ApplicationIPv6", _ipv6)
.WriteTo.MSSqlServer(connectionString, tableName /*, columnOptions: columnOptions*/)
.WriteTo
.Seq(ConfigurationManager.AppSettings["SerilogServer"])
.CreateLogger();
var file = File.CreateText("Self.log");
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
Run Code Online (Sandbox Code Playgroud)
但是在运行应用程序时文件访问错误是怎么回事。请在下面找到错误详细信息;
附加信息:该进程无法访问文件“C:\Program Files (x86)\IIS Express\Self.log”,因为它正被另一个进程使用。
谁可以帮我这个事
尝试
Serilog.Debugging.SelfLog.Enable(msg => File.AppendAllText (serilogSelfLogFilePath, msg));
用于写入文件的非锁定方式。serilogSelfLogFilePath是string您要使用的文件的有效路径。
不要忘记Log.CloseAndFlush();当您按照https://github.com/serilog/serilog/issues/864关闭其他日志时,否则它不会被写入
请注意,如果您使用的是异步接收器,那么当不同的线程争用该文件时,如果它变得繁忙,您最终会得到一个异常。