C# 如何附加到由";使用的文件;dotnet“;
当我开始在控制台应用程序上捕获并显示日志异常时,我意识到由于以下错误,有相当多的日志消息不存在 进程无法访问文件“[MY_LOG_file_PATH]”,因为它是 正在被另一个进程使用 这就是我在日志文件中记录消息的方式(我创建了一个新文件,每次运行应用程序时都会记录该文件)。变量C# 如何附加到由";使用的文件;dotnet“;,c#,visual-studio,file,process,C#,Visual Studio,File,Process,当我开始在控制台应用程序上捕获并显示日志异常时,我意识到由于以下错误,有相当多的日志消息不存在 进程无法访问文件“[MY_LOG_file_PATH]”,因为它是 正在被另一个进程使用 这就是我在日志文件中记录消息的方式(我创建了一个新文件,每次运行应用程序时都会记录该文件)。变量callingClass和callingMethod分别是调用Log函数的类和方法 try { var stackTrace = new StackTrace(); string callingMeth
callingClass
和callingMethod
分别是调用Log函数的类和方法
try
{
var stackTrace = new StackTrace();
string callingMethod = stackTrace.GetFrame(1).GetMethod().Name;
string callingClass = stackTrace.GetFrame(1).GetMethod().ReflectedType.Name;
string logText = string.Format(format, DateTime.Now, "INFO", callingClass, callingMethod, message);
if (!Directory.Exists("log"))
Directory.CreateDirectory("log");
if (!File.Exists(logFilePath))
File.Create(logFilePath);
using (FileStream f = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
using (StreamWriter sw = new StreamWriter(f))
sw.WriteLine(logText);
}
catch(IOException)
{
Console.WriteLine("Processes locking the file:");
var lockingProcesses = FileHelper.WhoIsLocking(logFilePath);
foreach (Process p in lockingProcesses)
Console.WriteLine("Process: " + p.ProcessName + " Machine:" + p.MachineName);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("The following log could not be written to file: " + message);
Console.WriteLine("Error: " + ex.Message);
Console.ResetColor();
}
当我捕获导致上述情况的IOException
时,我发现拥有日志文件的进程是驻留在机器“”上的“dotnet”,我假设它是当前机器
如何不丢失日志消息
IDE:Visual Studio Community 2019版本16.0.4
.NET Framework版本:4.7.03190
OS:Windows 10 Pro 64x尝试将您的邮件推送到共享BlockingCollection中,并有一个线程从中读取并写入日志文件。
文件。Create
创建并打开该文件。因此,在下一行中,当您尝试创建一个新的FileStream
文件时,该文件已经打开并抛出一个错误
一个选项是在创建文件流时立即调用Close
:
File.Create(logFilePath).Close();
另一种方法是只使用StreamWriter
打开/创建文件,然后跳过使用FileStream
。传递true
意味着附加到文件(如果存在)
try
{
var stackTrace = new StackTrace();
string callingMethod = stackTrace.GetFrame(1).GetMethod().Name;
string callingClass = stackTrace.GetFrame(1).GetMethod().ReflectedType.Name;
string logText = string.Format(format, DateTime.Now, "INFO", callingClass, callingMethod, message);
if (!Directory.Exists("log"))
Directory.CreateDirectory("log");
using (var sw = new StreamWriter(logFilePath, true))
{
sw.WriteLine(logText);
}
}
catch(IOException)
{
Console.WriteLine("Processes locking the file:");
var lockingProcesses = FileHelper.WhoIsLocking(logFilePath);
foreach (Process p in lockingProcesses)
Console.WriteLine("Process: " + p.ProcessName + " Machine:" + p.MachineName);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("The following log could not be written to file: " + message);
Console.WriteLine("Error: " + ex.Message);
Console.ResetColor();
}
你能用上面的代码给出一个更具体的例子吗?/另外,如果程序崩溃,
BlockingCollection
如何工作?您是否在其他地方打开了文件?您是否以管理员身份运行VS?您对该文件夹有任何权限限制吗?@Zinov是的,我有管理员权限,该文件夹不与任何其他人共享。我还尝试将包含日志文件的文件夹移动到桌面。请注意,这种情况并非总是发生。一些日志消息进入文件。该代码的上下文是什么,例如,您是否从多个线程调用它?代码的一个问题是file.Create
创建文件并打开它。因此,在下一行中,当您尝试创建一个新的FileStream
文件时,该文件已经打开并抛出一个错误。@ChrisDunaway Genius!你是对的,我真的无法猜测该文件应该在创建后关闭。请将此作为答案发布。