//-----------------------------------------------------------------------
//
// Copyright (c) 2009 Craig Sutherland. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Copyright (c) 2009 Craig Sutherland
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
namespace FastForward.WinCore
{
using System;
using System.IO;
using System.Xml.Linq;
///
/// Generates a log of an error.
///
public static class ErrorLogGenerator
{
#region Public methods
#region SaveLog()
///
/// Saves a log of the error to the default location.
///
/// The error to save.
/// The location the log was written to.
public static string SaveLog(this Exception error)
{
return SaveLogTo(error, GetDefaultLogLocation());
}
#endregion
#region SaveLogTo()
///
/// Saves a log of the error to the specified location.
///
/// The error to save.
/// The folder to save the log in.
/// The location the log was written to.
public static string SaveLogTo(this Exception error, string logLocation)
{
// Make sure there is an error to save
if (error == null)
{
throw new ArgumentNullException("error");
}
// Make sure there is a log location and it exists
if (string.IsNullOrEmpty(logLocation))
{
throw new ArgumentNullException("logLocation");
}
else if (!Directory.Exists(logLocation))
{
Directory.CreateDirectory(logLocation);
}
// Generate the log
var log = new XElement("errorLog",
new XElement("environment",
new XAttribute("time", DateTime.Now.ToString("o")),
new XAttribute("os", Environment.OSVersion.VersionString),
new XAttribute("processors", Environment.ProcessorCount),
new XAttribute("clr", Environment.Version)));
var thisError = error;
var parent = log;
while (thisError != null)
{
var errorEl = new XElement("error",
new XAttribute("type", error.GetType().FullName),
new XElement("message", error.Message),
new XElement("stack", error.StackTrace));
parent.Add(errorEl);
parent = errorEl;
thisError = thisError.InnerException;
}
// Save the log
var document = new XDocument(log);
var logFile = Path.Combine(logLocation, string.Format("Error{0:yyyyMMddHHmmss}.xml", DateTime.Now));
document.Save(logFile, SaveOptions.DisableFormatting);
return logFile;
}
#endregion
#region GetDefaultLogLocation()
///
/// Gets the default log location.
///
/// The default log location.
public static string GetDefaultLogLocation()
{
var location = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Path.Combine("FastForward.NET", "ErrorLogs"));
return location;
}
#endregion
#endregion
}
}