本文介绍了如果不使用Mutex类我必须写在日志中不同aplications的xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void WriteXmlLog(string logType, string logFlag, string logModule, string logLocation, string logText, string logStackTrace)
    {
        Mutex objMutex = new Mutex(false, @"Global\MySharedLog");
        objMutex.WaitOne();
        try
        {
            if(!File.Exists(_logFilePath))
            {
                File.WriteAllText(_logFilePath, "<?xml version='1.0' encoding='utf-8' standalone='yes'?>\r\n <AppXmlLogWritter></AppXmlLogWritter>");
            }

            string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(_logFilePath);
            XmlElement newelement = xmlDoc.CreateElement("LogData");
            XmlElement xmlLogID = xmlDoc.CreateElement("LogID");
            XmlElement xmlLogDateTime = xmlDoc.CreateElement("LogDateTime");
            XmlElement xmlLogType = xmlDoc.CreateElement("LogType");
            XmlElement xmlLogFlag = xmlDoc.CreateElement("LogFlag");
            XmlElement xmlLogApplication = xmlDoc.CreateElement("LogApplication");
            XmlElement xmlLogModule = xmlDoc.CreateElement("LogModule");
            XmlElement xmlLogLocation = xmlDoc.CreateElement("LogLocation");
            XmlElement xmlLogText = xmlDoc.CreateElement("LogText");
            XmlElement xmlLogStackTrace = xmlDoc.CreateElement("LogStackTrace");

            xmlLogID.InnerText = _logIDPrefix + currentDateTime + randomNumber;
            xmlLogDateTime.InnerText = currentDateTime;
            xmlLogType.InnerText = ((LogTypes)Convert.ToInt32(logType)).ToString();
            xmlLogFlag.InnerText = logFlag;
            xmlLogApplication.InnerText = _logApplication;
            xmlLogModule.InnerText = logModule;
            xmlLogLocation.InnerText = logLocation;
            xmlLogText.InnerText = logText;
            xmlLogStackTrace.InnerText = logStackTrace;

            newelement.AppendChild(xmlLogID);
            newelement.AppendChild(xmlLogDateTime);
            newelement.AppendChild(xmlLogType);
            newelement.AppendChild(xmlLogFlag);
            newelement.AppendChild(xmlLogApplication);
            newelement.AppendChild(xmlLogModule);
            newelement.AppendChild(xmlLogLocation);
            newelement.AppendChild(xmlLogText);

            xmlDoc.DocumentElement.AppendChild(newelement);
            xmlDoc.Save(_logFilePath);

        }
        finally
        {
            objMutex.ReleaseMutex();
        }
    }

我写在几个不同的应用程序的XML文件日志。
        见下文code I M使用Mutex类锁定的目的指当两个线程是在一个时间mutex.waitone()方法,如果第一个线程执行的任务将不会释放第二个线程。
         是否有可能不使用Mutex类,我必须写日志不同aplications的xml文件

I am writing logs in xml file of several different applications. see below code i m using Mutex class for locking purpose Means when two thread comes at a time mutex.waitone() method wouldn't release second thread if first thread doing task. Is it possible without using Mutex class i have to write log in xml file of different aplications

推荐答案

互斥是一个标准的技术来同步访问多个进程共享的资源。如果是在同一进程中的并发访问,你也可以使用更轻巧的类,如的。但是,因为你需要支持跨进程同步互斥是要走的路。

Mutex is a standard technique to synchronize access to a shared resource across multiple processes. If it was concurrent access within the same process you could have used more lightweight classes such as ReaderWriterLockSlim. But since you need to support cross process synchronizations Mutexes is the way to go.

也许开始问自己是否写日志到一个文本文件,在这种情况下适当的。你有没有考虑到记录将处理并发为你的事件日志?你有没有考虑记录到一个共享的数据库也将处理并发的吗?

Or maybe start asking yourself whether writing logs to a text file is appropriate in this case. Have you considered logging to the EventLog which will handle concurrency for you? Have you considered logging to a shared database which also will handle the concurrency for you?

顺便说一句你考虑使用记录库来执行你的日志,而不是用手工做一些的XmlDocument ? .NET已经内置了跟踪功能。也许你应该这样滚动定制的解决方案之前,探索它们。

By the way have you considered using a logging library to perform your logging instead of manually doing it with some XmlDocument? .NET already has built-in tracing capabilities. Maybe you should explore them before rolling such custom solutions.

很多问题你应该问自己了。

Many questions you should probably be asking yourself now.

这篇关于如果不使用Mutex类我必须写在日志中不同aplications的xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:07