C# 向我的应用程序添加可以同时从不同EXE文件写入的日志
我有一个C# 向我的应用程序添加可以同时从不同EXE文件写入的日志,c#,.net,multithreading,C#,.net,Multithreading,我有一个命令行应用程序从用户处接收文件(DOC,PDF),该文件位于同一台计算机上,我的应用程序将该文件复制到特定文件夹,并返回0表示此操作已通过,返回1表示否。 此命令行exe文件可以并发打开多次,并且没有问题。 现在我想在我的应用程序中添加一个日志,该日志将位于应用程序文件夹中,该日志将写入每个文件名以及操作是否通过或失败。 现在我想知道,如果我有几个打开的进程,如何实现这一点,以及如何避免2个exe文件同时尝试写入我的日志。 在这种情况下,虽然我同时使用了多个exe文件,但我是否可以使用l
命令行
应用程序从用户处接收文件(DOC,PDF),该文件位于同一台计算机上,我的应用程序将该文件复制到特定文件夹,并返回0表示此操作已通过,返回1表示否。
此命令行exe文件
可以并发打开多次,并且没有问题。
现在我想在我的应用程序中添加一个日志
,该日志将位于应用程序文件夹中,该日志
将写入每个文件名以及操作是否通过或失败。
现在我想知道,如果我有几个打开的进程,如何实现这一点,以及如何避免2个exe文件同时尝试写入我的日志。
在这种情况下,虽然我同时使用了多个exe文件
,但我是否可以使用lock
。使用ETW并登录到windows机制。事件日志不是新的(它已经存在很长时间了),ETW现在通过nuget软件包得到了完全支持
ETW也是基于内核的。不要创建日志文件。使用ETW并登录到windows机制。事件日志不是新的(它已经存在很长时间了),ETW现在通过nuget软件包得到了完全支持
ETW也是基于内核的。不要创建日志文件。使用ETW并登录到windows机制。事件日志不是新的(它已经存在很长时间了),ETW现在通过nuget软件包得到了完全支持
ETW也是基于内核的。不要创建日志文件。使用ETW并登录到windows机制。事件日志不是新的(它已经存在很长时间了),ETW现在通过nuget软件包得到了完全支持
ETW也是基于内核的。您可以创建命名系统互斥体来控制对日志文件的访问
// Set this variable to false if you do not want to request
// initial ownership of the named mutex.
bool requestInitialOwnership = true;
bool mutexWasCreated;
// Request initial ownership of the named mutex by passing
// true for the first parameter. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object. If "MyMutex" is created by this call,
// then mutexWasCreated contains true; otherwise, it contains
// false.
Mutex m = new Mutex(requestInitialOwnership, "MyMutex", out mutexWasCreated);
为了确保有指定的互斥体,您可以使用mutex.TryOpenExisting(“MyMutex”,resultMutex)
,如果它存在,您可以等待,记录并释放它
resultMutex.WaitOne();
Log("success");
resultMutex.ReleaseMutex();
MSDN中提供了更多信息:您可以创建命名系统互斥来控制对日志文件的访问
// Set this variable to false if you do not want to request
// initial ownership of the named mutex.
bool requestInitialOwnership = true;
bool mutexWasCreated;
// Request initial ownership of the named mutex by passing
// true for the first parameter. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object. If "MyMutex" is created by this call,
// then mutexWasCreated contains true; otherwise, it contains
// false.
Mutex m = new Mutex(requestInitialOwnership, "MyMutex", out mutexWasCreated);
为了确保有指定的互斥体,您可以使用mutex.TryOpenExisting(“MyMutex”,resultMutex)
,如果它存在,您可以等待,记录并释放它
resultMutex.WaitOne();
Log("success");
resultMutex.ReleaseMutex();
MSDN中提供了更多信息:您可以创建命名系统互斥来控制对日志文件的访问
// Set this variable to false if you do not want to request
// initial ownership of the named mutex.
bool requestInitialOwnership = true;
bool mutexWasCreated;
// Request initial ownership of the named mutex by passing
// true for the first parameter. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object. If "MyMutex" is created by this call,
// then mutexWasCreated contains true; otherwise, it contains
// false.
Mutex m = new Mutex(requestInitialOwnership, "MyMutex", out mutexWasCreated);
为了确保有指定的互斥体,您可以使用mutex.TryOpenExisting(“MyMutex”,resultMutex)
,如果它存在,您可以等待,记录并释放它
resultMutex.WaitOne();
Log("success");
resultMutex.ReleaseMutex();
MSDN中提供了更多信息:您可以创建命名系统互斥来控制对日志文件的访问
// Set this variable to false if you do not want to request
// initial ownership of the named mutex.
bool requestInitialOwnership = true;
bool mutexWasCreated;
// Request initial ownership of the named mutex by passing
// true for the first parameter. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object. If "MyMutex" is created by this call,
// then mutexWasCreated contains true; otherwise, it contains
// false.
Mutex m = new Mutex(requestInitialOwnership, "MyMutex", out mutexWasCreated);
为了确保有指定的互斥体,您可以使用mutex.TryOpenExisting(“MyMutex”,resultMutex)
,如果它存在,您可以等待,记录并释放它
resultMutex.WaitOne();
Log("success");
resultMutex.ReleaseMutex();
MSDN中提供了更多信息:如果您不太关心日志的清洁度,可以通过允许多个进程写入的方式打开日志文件:
private static Stream CreateFile(string path, bool checkHost)
{
var mode = FileMode.Append;
return new FileStream(path, mode, FileAccess.Write, FileShare.ReadWrite, 4096, FileOptions.SequentialScan, Path.GetFileName(path), false, false, checkHost);
}
缺点是,如果两个进程同时写入文件,则会出现如下混乱:
2014-11-26 11:32:93 Suc2014-11-26 11:32:93失败:文件“Some.doc”
种子:文件“Other.doc”
也就是说,进程之间相互竞争,最终会出现混杂的日志条目。如果没有太多的进程写入同一个日志文件,并且每个进程很少写入日志文件,那么应该很少发生这样的冲突
有几种方法可以解决这个问题。一种是在每个进程中以只读模式打开文件,然后等待其他进程,直到文件可用;最简单的方法是在while循环中尝试捕获文件.AppendAllText()
,直到成功。问题中列出了其他选项,如
另一种方法是将日志写入多个文件,或写入文件以外的其他文件,例如数据库。如果您不太关心日志的清洁度,可以通过允许多个进程写入的方式打开日志文件:
private static Stream CreateFile(string path, bool checkHost)
{
var mode = FileMode.Append;
return new FileStream(path, mode, FileAccess.Write, FileShare.ReadWrite, 4096, FileOptions.SequentialScan, Path.GetFileName(path), false, false, checkHost);
}
缺点是,如果两个进程同时写入文件,则会出现如下混乱:
2014-11-26 11:32:93 Suc2014-11-26 11:32:93失败:文件“Some.doc”
种子:文件“Other.doc”
也就是说,进程之间相互竞争,最终会出现混杂的日志条目。如果没有太多的进程写入同一个日志文件,并且每个进程很少写入日志文件,那么应该很少发生这样的冲突
有几种方法可以解决这个问题。一种是在每个进程中以只读模式打开文件,然后等待其他进程,直到文件可用;最简单的方法是在while循环中尝试捕获文件.AppendAllText()
,直到成功。问题中列出了其他选项,如
另一种方法是将日志写入多个文件,或写入文件以外的其他文件,例如数据库。如果您不太关心日志的清洁度,可以通过允许多个进程写入的方式打开日志文件:
private static Stream CreateFile(string path, bool checkHost)
{
var mode = FileMode.Append;
return new FileStream(path, mode, FileAccess.Write, FileShare.ReadWrite, 4096, FileOptions.SequentialScan, Path.GetFileName(path), false, false, checkHost);
}
缺点是,如果两个进程同时写入文件,则会出现如下混乱:
2014-11-26 11:32:93 Suc2014-11-26 11:32:93失败:文件“Some.doc”
种子:文件“Other.doc”
也就是说,进程之间相互竞争,最终会出现混杂的日志条目。如果没有太多的进程写入同一个日志文件,并且每个进程很少写入日志文件,那么应该很少发生这样的冲突
有几种方法可以解决这个问题。一种是在每个进程中以只读模式打开文件,然后等待其他进程,直到文件可用;最简单的方法是在while循环中尝试捕获文件.AppendAllText()
,直到成功。问题中列出了其他选项,如
另一种选择是西铁