/// <summary> /// Logs an error to the database. /// </summary> /// <remarks> /// Logs an error as a single XML file stored in a folder. XML files are named with a /// sortable date and a unique identifier. Currently the XML files are stored indefinately. /// As they are stored as files, they may be managed using standard scheduled jobs. /// </remarks> public override string Log(Error error) { var logPath = LogPath; if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } var errorId = Guid.NewGuid().ToString(); var timeStamp = (error.Time > DateTime.MinValue ? error.Time : DateTime.Now); var fileName = string.Format(CultureInfo.InvariantCulture, @"error-{0:yyyy-MM-ddHHmmssZ}-{1}.xml", /* 0 */ timeStamp.ToUniversalTime(), /* 1 */ errorId); var path = Path.Combine(logPath, fileName); using (var writer = new XmlTextWriter(path, Encoding.UTF8)) { writer.Formatting = Formatting.Indented; writer.WriteStartElement("error"); writer.WriteAttributeString("errorId", errorId); ErrorXml.Encode(error, writer); writer.WriteEndElement(); writer.Flush(); } return(errorId); }
/// <summary> /// Logs an error to the database. /// </summary> /// <remarks> /// Logs an error as a single XML file stored in a folder. XML files are named with a /// sortable date and a unique identifier. Currently the XML files are stored indefinately. /// As they are stored as files, they may be managed using standard scheduled jobs. /// </remarks> public override string Log(Error error) { string errorId = Guid.NewGuid().ToString(); string timeStamp = DateTime.UtcNow.ToString("yyyy-MM-ddHHmmssZ", CultureInfo.InvariantCulture); string path = Path.Combine(LogPath, string.Format(@"error-{0}-{1}.xml", timeStamp, errorId)); XmlTextWriter writer = new XmlTextWriter(path, Encoding.UTF8); try { writer.Formatting = Formatting.Indented; writer.WriteStartElement("error"); writer.WriteAttributeString("errorId", errorId); ErrorXml.Encode(error, writer); writer.WriteEndElement(); writer.Flush(); } finally { writer.Close(); } return(errorId); }
public void ProcessRequest(HttpContext context) { HttpResponse response = context.Response; response.ContentType = "application/xml"; // // Retrieve the ID of the requested error and read it from // the store. // string errorId = Mask.NullString(context.Request.QueryString["id"]); if (errorId.Length == 0) { throw new ApplicationException("Missing error identifier specification."); } ErrorLogEntry entry = ErrorLog.GetDefault(context).GetError(errorId); // // Perhaps the error has been deleted from the store? Whatever // the reason, pretend it does not exist. // if (entry == null) { throw new HttpException((int)HttpStatusCode.NotFound, string.Format("Error with ID '{0}' not found.", errorId)); } // // Stream out the error as formatted XML. // #if !NET_1_0 && !NET_1_1 XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; settings.CheckCharacters = false; XmlWriter writer = XmlWriter.Create(response.Output, settings); #else XmlTextWriter writer = new XmlTextWriter(response.Output); writer.Formatting = Formatting.Indented; #endif writer.WriteStartDocument(); writer.WriteStartElement("error"); ErrorXml.Encode(entry.Error, writer); writer.WriteEndElement(/* error */); writer.WriteEndDocument(); writer.Flush(); }
/// <summary> /// Logs an error to the database. /// </summary> /// <remarks> /// Logs an error as a single XML file stored in a folder. XML files are named with a /// sortable date and a unique identifier. Currently the XML files are stored indefinately. /// As they are stored as files, they may be managed using standard scheduled jobs. /// </remarks> public override string Log(Error error) { var logPath = LogPath; if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } var errorId = Guid.NewGuid().ToString(); var timeStamp = (error.Time > DateTime.MinValue ? error.Time : DateTime.Now); var fileName = string.Format(CultureInfo.InvariantCulture, @"error-{0:yyyy-MM-ddHHmmssZ}-{1}.xml", /* 0 */ timeStamp.ToUniversalTime(), /* 1 */ errorId); var path = Path.Combine(logPath, fileName); try { using (var writer = new XmlTextWriter(path, Encoding.UTF8)) { writer.Formatting = Formatting.Indented; writer.WriteStartElement("error"); writer.WriteAttributeString("errorId", errorId); ErrorXml.Encode(error, writer); writer.WriteEndElement(); writer.Flush(); } } catch (IOException) { // If an IOException is thrown during writing the file, // it means that we will have an either empty or // partially written XML file on disk. In both cases, // the file won't be valid and would cause an error in // the UI. File.Delete(path); throw; } return(errorId); }