需求如下:
1.每条Log 有4个信息,时间,级别,类名(取每个调用该Log库的类的类名),内容
2.既然是同一个文件供多个进程来写,自然要实现写互斥 
3.写log时先判断文件是否存在,不存在自己生成一个,存在的话直接打开请问C#里有没有直接支持上面需求的Log类 (就像java的Log4j一样)
如果没有,请问各位高手,针对我上面提的这些需求,有什么简便的设计方法么?

解决方案 »

  1.   

    public static Mutex PubMutex=new Mutex();
    public static Mutex LogMutex=new Mutex();
    private clsConst LogPen = new clsConst();private void WriteLog(string LogMsg)
    {   //根据时间每天创建一个日志文件
        try
       {
    LogMutex.WaitOne();
    string logFilePath = "AlarmMeslogs\\";
    string LogFileName = DateTime.Today.ToString("yyyyMMdd")+ DateTime.Now.Hour.ToString()+".Log";

    string filepath=logFilePath+LogFileName;
    if(!(Directory.Exists(logFilePath)))
    Directory.CreateDirectory(logFilePath); 
    StreamWriter sw=new StreamWriter(filepath,true,Encoding.Unicode);
    sw.WriteLine(DateTime.Now .ToLongTimeString() + " " + LogMsg);
    sw.Close();
    }
    catch(Exception error)
    {
    LogPen.WriteLog("Result - Adding Items:"+error.Message+"【AlarmWriteLog()】");
    }
    finally
    {
    LogMutex.ReleaseMutex();
    }
    }
      

  2.   

    利用文件本身的锁来实现互斥
    如果读或写失败,将log信息存入cache
    略过此次读写
    待文件可访问时再写入进去