public class Logwriter {
public Void WriteXml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\Log_Data.xml");
XmlElement newelement = xmlDoc.CreateElement("entry");
XmlElement xmldata = xmlDoc.CreateElement("data");
XmlElement xmlcontent = xmlDoc.CreateElement("content");
xmldata.InnerText ="1234" ;
xmlcontent.InnerText ="Stackoverflow";
newelement.AppendChild(xmldata);
newelement.AppendChild(xmlcontent);
xmlDoc.DocumentElement.AppendChild(newelement);
xmlDoc.Save(@"C:\Log_Data.xml");
}}
Above WriteXml() function can be called by 100 applications at a time so i have to prevent IOException Error.
I can avod these exception using lock so how can i do it.
please explain which object i have to lock during writing xml
It sounds like you are talking about a named mutex; you can use a
Mutexwith a name likeGlobal\MySharedLog, which will then be visible to different applications in different sessions on the same machine. This allows each exe to take an exclusive lock (in a managed way) for a period of time.However!
For example: