我用两个控制台程序,各循环10000次,
引用同一个LOg4Net.DLL,并且对同一个文件同时写入Log。
每次循环写4行:
logger.Info(i + " log_test2");
logger.Info("程序已经启动");
logger.Info("程序没有错误");
logger.Info("程序开始调试");最后运行完,为什么数据行数不够呢
哪位知道?
引用同一个LOg4Net.DLL,并且对同一个文件同时写入Log。
每次循环写4行:
logger.Info(i + " log_test2");
logger.Info("程序已经启动");
logger.Info("程序没有错误");
logger.Info("程序开始调试");最后运行完,为什么数据行数不够呢
哪位知道?
2:如果两个应用程序都是你自己的程序,你非要这么做,可以使用进程间锁Mutex,在对文件进行写入的时候锁定,估计就能满足你的要求了
[email protected]
bool requestInitialOwnership = true;
bool mutexWasCreated;
Mutex m = new Mutex(requestInitialOwnership,
"MyMutex ",
out mutexWasCreated);
if (!(requestInitialOwnership && mutexWasCreated))
{
//Console.WriteLine("Waiting for the named mutex. ");
m.WaitOne();
} //Console.WriteLine("This process owns the named mutex. " +
// "Press ENTER to release the mutex and exit. ");
//Console.ReadLine(); log4net.ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
for (int i = 0; i < 10000; i++)
{
logger.Info(i + " log_test2");
logger.Info("程序已经启动");
logger.Info("程序没有错误");
logger.Info("程序开始调试");
} m.ReleaseMutex();
按照luminji的方法,我套了一层mutex,结果是对了。
谢谢大家