我试图找出登录例外时的最佳做法。
到目前为止,我每次捕获异常时都会记录。但是当一个较低的杠杆类捕获异常(例如,从数据库层),并将其包装在我们自己的应用程序异常中时 - 我是否还要在那里记录原始异常,或者我应该让上级杠杆类记录所有细节?
那么由于输入参数错误,我的低级别类决定抛出异常的位置呢?它是否应该在那里记录异常,或者再一次让捕获代码记录下来?
答案 0 :(得分:6)
主要是你应该避免在较低级别的catch和较高级别的catch中记录它,因为这会使日志充满冗余信息(更不用说占用额外的IO资源来写入日志)。
如果您正在寻找有关异常处理的一般最佳做法信息,请this link is handy。
答案 1 :(得分:4)
只要您的日志记录代码(a)记录异常的堆栈跟踪,并且(b)记录整个内部异常链,您就可以在应用程序的最顶层只记录一次
Microsoft异常处理应用程序块为您处理这两件事。我猜其他日志框架也会这样做。
答案 2 :(得分:0)
在我的winform的应用程序中,我创建了一些Observer用于记录。 Observer有订阅者,可以在某处写日志或处理它。 它看起来:
public static class LoggingObserver
{
/// <summary>
/// Last getted log message
/// </summary>
public static string LastLog;
/// <summary>
/// Last getted exception
/// </summary>
public static Exception LastException;
/// <summary>
/// List of log's processors
/// </summary>
public static List<BaseLogging> loggings = new List<BaseLogging>();
/// <summary>
/// Get Exception and send for log's processors
/// </summary>
/// <param name="ex">Exception with message</param>
public static void AddLogs(Exception ex)
{
LastException = ex;
LastLog = string.Empty;
foreach (BaseLogging logs in loggings)
{
logs.AddLogs(ex);
}
}
/// <summary>
/// Get message log for log's processors
/// </summary>
/// <param name="str">Message log</param>
public static void AddLogs(string str)
{
LastException = null;
LastLog = str;
foreach (BaseLogging logs in loggings)
{
logs.AddLogs(str);
}
}
/// <summary>
/// Close all processors
/// </summary>
public static void Close()
{
foreach (BaseLogging logs in loggings)
{
logs.Close();
}
}
}
订阅者的抽象类:
public abstract class BaseLogging
{
/// <summary>
/// Culture (using for date)
/// </summary>
public CultureInfo culture;
/// <summary>
/// Constructor
/// </summary>
/// <param name="culture">Culture</param>
public BaseLogging(CultureInfo culture)
{
this.culture = culture;
}
/// <summary>
/// Add log in log system
/// </summary>
/// <param name="str">message of log</param>
public virtual void AddLogs(string str)
{
DateTime dt = DateTime.Now;
string dts = Convert.ToString(dt, culture.DateTimeFormat);
WriteLine(String.Format("{0} : {1}", dts, str));
}
/// <summary>
/// Add log in log system
/// </summary>
/// <param name="ex">Exception</param>
public virtual void AddLogs(Exception ex)
{
DateTime dt = DateTime.Now;
string dts = Convert.ToString(dt, culture.DateTimeFormat);
WriteException(ex);
}
/// <summary>
/// Write string on log system processor
/// </summary>
/// <param name="str">logs message</param>
protected abstract void WriteLine(string str);
/// <summary>
/// Write string on log system processor
/// </summary>
/// <param name="ex">Exception</param>
protected abstract void WriteException(Exception ex);
/// <summary>
/// Close log system (file, stream, etc...)
/// </summary>
public abstract void Close();
}
记录到文件的实现:
/// <summary>
/// Logger processor, which write log to some stream
/// </summary>
public class LoggingStream : BaseLogging
{
private Stream stream;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="stream">Initialized stream</param>
/// <param name="culture">Culture of log system</param>
public LoggingStream (Stream stream, CultureInfo culture)
: base(culture)
{
this.stream = stream;
}
/// <summary>
/// Write message log to stream
/// </summary>
/// <param name="str">Message log</param>
protected override void WriteLine(string str)
{
try
{
byte[] bytes;
bytes = Encoding.ASCII.GetBytes(str + "\n");
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
}
catch { }
}
/// <summary>
/// Write Exception to stream
/// </summary>
/// <param name="ex">Log's Exception</param>
protected override void WriteException(Exception ex)
{
DateTime dt = DateTime.Now;
string dts = Convert.ToString(dt, culture.DateTimeFormat);
string message = String.Format("{0} : Exception : {1}", dts, ex.Message);
if (ex.InnerException != null)
{
message = "Error : " + AddInnerEx(ex.InnerException, message);
}
WriteLine(message);
}
/// <summary>
/// Closing stream
/// </summary>
public override void Close()
{
stream.Close();
}
private string AddInnerEx(Exception exception, string message)
{
message += "\nInner Exception : " + exception.Message;
if (exception.InnerException != null)
{
message = AddInnerEx(exception.InnerException, message);
}
return message;
}
}
使用:
//initialization
FileStream FS = new FileStream(LogFilePath, FileMode.Create);
LoggingObserver.loggings.Add(new LoggingStream(FS, Thread.CurrentThread.CurrentCulture));
//write exception
catch (Exception ex) {
LoggingObserver.AddLog(new Exception ("Exception message", ex));
}
//write log
LoggingObserver.AddLog("Just a log");
答案 3 :(得分:-1)
记录你抓到的地方,如果你正在包装那么你应该。如果下层包装器没有那么你有理由(为了可调试性)这样做。但是,除非你知道它的良性或者你可以处理它,否则不要吞下异常。
我建议
try{
.
.
.
} catch(Exception ex){
... log ....
throw;
}
如果您需要记录并传递异常。