我们正在将serilog与elasticsearch接收器一起使用来记录我们的应用程序消息,有时会有问题记录到Elasticsearch中,我们使用SelfLog
将这些问题记录到本地日志文件中
Serilog.Debugging.SelfLog.Enable((s) =>
{
using (var errorStreamWriter = TextWriter.Synchronized(File.AppendText($"{loggingErrorDirectory}{Path.DirectorySeparatorChar}loggingErrors.{DateTime.Today:yyyy.MM.dd}.log")))
{
errorStreamWriter.WriteLine(s);
}
}
最近,当我们将这些记录错误记录到本地文件时,我们开始看到IOException
{
"Depth": 0,
"ClassName": "System.IO.IOException",
"Message": "The process cannot access the file 'C:\\ResultFilesAndErrors\\loggingErrors.2018.12.05.log' because it is being used by another process.",
"Source": "mscorlib",
"StackTraceString": " at
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n at
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)\r\n at
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)\r\n at
System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)\r\n at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)\r\n at
System.IO.StreamWriter..ctor(String path, Boolean append)\r\n at
System.IO.File.AppendText(String path)\r\n at
Taf.Logger.SeriLogger.<>c__DisplayClass17_0.<Setup>b__2(String s) in C:\\Projects\\trunkn3\\core\\src\\ABC.Logger.Serilog\\SeriLogger.cs:line 88\r\n at
Serilog.Debugging.SelfLog.WriteLine(String format, Object arg0, Object arg1, Object arg2)\r\n at
Serilog.Core.Logger.Dispatch(LogEvent logEvent)\r\n at
Serilog.Core.Logger.Serilog.Core.ILogEventSink.Emit(LogEvent logEvent)\r\n at
Serilog.Core.Logger.Dispatch(LogEvent logEvent)\r\n at
Serilog.Core.Logger.Serilog.Core.ILogEventSink.Emit(LogEvent logEvent)\r\n at
Serilog.Core.Logger.Dispatch(LogEvent logEvent)\r\n at
Serilog.Core.Logger.Serilog.Core.ILogEventSink.Emit(LogEvent logEvent)\r\n at
Serilog.Core.Logger.Dispatch(LogEvent logEvent)\r\n at
Serilog.Core.Logger.Write(LogEventLevel level, Exception exception, String messageTemplate, Object[] propertyValues)\r\n at
Serilog.Core.Logger.Verbose(String messageTemplate, Object[] propertyValues)\r\n at
**** removed my library calls****",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": {
"Name": "WinIOError",
"AssemblyName": "mscorlib",
"AssemblyVersion": "4.0.0.0",
"AssemblyCulture": "",
"ClassName": "System.IO.__Error",
"Signature": "Void WinIOError(Int32, System.String)",
"MemberType": 8
},
"HResult": -2147024864,
"HelpURL": null
}
我们的应用程序是多线程的,我想知道这是否可能是这些异常的原因(我认为我们会受到TextWriter.Synchronized
的保护)。