private bool disposedValue = false; // To detect redundant calls void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects). try { // Attempt to save the log file if (LogDocument != null && OutputPath != string.Empty) { using (FileStream fs = new FileStream(OutputPath, FileMode.Create)) { LogDocument.Save(fs); // Cleardown items to prevent file lock. fs.Flush(); fs.Dispose(); } } } finally { LogDocument = null; } } // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. // TODO: set large fields to null. disposedValue = true; } }
/// <summary> /// Write the XML log entry. /// </summary> /// <typeparam name="TState"></typeparam> /// <param name="logLevel"></param> /// <param name="eventId"></param> /// <param name="state"></param> /// <param name="exception"></param> /// <param name="formatter"></param> void ILogger.Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { XmlElement xEleNew = null; XmlNode xLogEntries = null; XmlAttribute xa = null; long rowCount = 0; // only log at the specified level and below if ((int)this.LogLevel <= (int)logLevel && logLevel != LogLevel.None) { // Make sure requests do not bump into each other. while (Busy == true) { System.Threading.Thread.Sleep(250); } Busy = true; try { LogDocument = this.LoadOrCreateLogDocument(OutputPath); // Get row information from file. xLogEntries = (from XmlNode xn in LogDocument.ChildNodes where xn.Name == "logEntries" select xn).First(); rowCount = Convert.ToInt64(xLogEntries.Attributes.GetNamedItem(@"entryCount").Value); if (rowCount > MaxLogRows) { this.LogRotate(); // get the important info again. xLogEntries = null; xLogEntries = (from XmlNode xn in LogDocument.ChildNodes where xn.Name == "logEntries" select xn).First(); rowCount = Convert.ToInt64(xLogEntries.Attributes.GetNamedItem(@"entryCount").Value); } // Write the element items xEleNew = LogDocument.CreateElement(@"logEntry"); xa = LogDocument.CreateAttribute("id"); xa.Value = rowCount.ToString(); xEleNew.Attributes.Append(xa); // Create the object data and serialise it. LogEntryData le = this.Create_EntryData(logLevel, eventId, state, exception, formatter); using (XMLSerializer serializer = new XMLSerializer()) { xEleNew.InnerXml += serializer.Serialize(le.GetType(), le, @"logEntry").InnerXml; } rowCount++; xLogEntries.Attributes.GetNamedItem(@"entryCount").Value = rowCount.ToString(); xLogEntries.AppendChild(xEleNew); try { using (FileStream fs = new FileStream(OutputPath, FileMode.OpenOrCreate, FileAccess.Write)) { LogDocument.Save(fs); // Cleardown items to prevent file lock. fs.Flush(); fs.Dispose(); } } finally { LogDocument = null; } } catch (Exception ex) { // TODO own exceptions are being logged! // throw new LoggerException(@"Something went wrong while logging using " + this.GetType().ToString() + " see inner exception for details.", ex); } finally { Busy = false; xEleNew = null; xLogEntries = null; xa = null; } } }