如何将错误日志或异常写入C#.NET中的文件
我需要帮助将错误写入单独的文本文件,并通过电子邮件将该文件发送给我。实际上,我已经创建了windows应用程序,但我并没有在单独的文件中添加任何异常或写入错误。现在,我想每当我的应用程序因为任何错误而中断,然后错误应该被记录到单独的文本文件中,该文件应该是电子邮件给我 我对错误日志文件进行了研究,我发现可以创建单独的类将错误日志写入文本文件,并将该文件调用到所有方法的try-catch块中。但是我的程序/应用程序已经变得非常庞大,我需要添加它的中间部分。另外,我发现了log4net,但它仍然以同样的方式工作 有谁能指导我如何在应用程序级别或当应用程序收到任何错误消息时编写错误日志如何将错误日志或异常写入C#.NET中的文件,c#,C#,我需要帮助将错误写入单独的文本文件,并通过电子邮件将该文件发送给我。实际上,我已经创建了windows应用程序,但我并没有在单独的文件中添加任何异常或写入错误。现在,我想每当我的应用程序因为任何错误而中断,然后错误应该被记录到单独的文本文件中,该文件应该是电子邮件给我 我对错误日志文件进行了研究,我发现可以创建单独的类将错误日志写入文本文件,并将该文件调用到所有方法的try-catch块中。但是我的程序/应用程序已经变得非常庞大,我需要添加它的中间部分。另外,我发现了log4net,但它仍然以同
谢谢您的帮助。如果您尚未连接到任何系统,我建议您查看,因为它可以满足您的要求,而且是一个很受欢迎的库,因此它将被您团队中的其他专业人员识别和扩展。另外,由于其他人维护,您偶尔会得到免费的bug修复。据我所知,您所需要的只是处理。它将为您提供有关崩溃的最常见信息。你可以用它来开始研究撞车原因 当然,在
UnhandledException
处理程序中,您可以使用任何您想要的,log4net
或自定义记录器 试试看
< /P> < P>若要处理其他地方未捕获的所有异常,请考虑为
< P>实现处理程序,我为我的WPF项目创建了一个日志帮助器。这是一个包含许多不同组件的大型项目。这是在我学会如何使用MVVM之前,所以如果这打破了模式,请原谅。该系统正在不断扩展,使用新的组件来处理不同的任务。您可以将组件视为处理特定任务的独立单元,其中主组件是所有组件的主组件。这种日志记录允许我们记录错误,无论它们发生在哪个组件中。这可能有点过分,但对我们来说效果很好 我使用相同的原理记录所有系统范围的事件(用户操作、数据更改等) 在我的App.xaml.cs中,我捕获所有未处理的错误。注意,这是日志记录的最后手段,我在任何我希望出现异常的地方使用try-catch(例如db连接) ErrorEvent是my Utilities dll中的一个类,它使用事件路由记录错误。我有不同类型的错误,这取决于严重性。所有这些错误都记录到一个数据库中,唯一知道数据库连接的组件是主组件。这就是为什么我使用路由事件将异常发送到要记录的主组件。ErrorEventHelper将被填充并路由到主组件。在使用try-catch的情况下,我创建EventHelper并使用LogErrorEvent方法将其传递给日志。ShowMessage属性用于决定是通知用户异常,还是只记录异常public class ErrorEvent : UserControl
{
public delegate void ErrorEventEventHandler(object sender, RoutedCustomEventArgs e);
public static readonly RoutedEvent EventOccuredEvent = EventManager.RegisterRoutedEvent(
"EventOccured", RoutingStrategy.Bubble, typeof(ErrorEventEventHandler), typeof(ErrorEvent));
// Provide CLR accessors for the event
public event RoutedEventHandler EventOccured
{
add { AddHandler(EventOccuredEvent, value); }
remove { RemoveHandler(EventOccuredEvent, value); }
}
public void LogErrorEvent(ErrorEventHelper occuredEventDetails)
{
RoutedEventArgs newEventArgs = new RoutedCustomEventArgs(ErrorEvent.EventOccuredEvent, occuredEventDetails);
RaiseEvent(newEventArgs);
}
public void LogErrorEvent(Exception exception)
{
ErrorEventHelper occuredEventDetails = new ErrorEventHelper();
occuredEventDetails.ErrorType = ErrorEventHelper.EventTypes.Critical;
occuredEventDetails.ErrorValue = exception.Message;
occuredEventDetails.ErrorStack = exception.StackTrace;
occuredEventDetails.ShowMessage = true;
RoutedEventArgs newEventArgs = new RoutedCustomEventArgs(ErrorEvent.EventOccuredEvent, occuredEventDetails);
RaiseEvent(newEventArgs);
if (exception.InnerException != null)
{
LogErrorEvent(exception.InnerException);
}
}
public class RoutedCustomEventArgs : RoutedEventArgs
{
public ErrorEventHelper OccuredEventDetails { get; private set; }
public RoutedCustomEventArgs(RoutedEvent routedEvent, ErrorEventHelper occuredEventDetails)
: base(routedEvent)
{
this.OccuredEventDetails = occuredEventDetails;
}
}
}
public class ErrorEventHelper
{
public string ErrorValue { get; set; }
private EventTypes _ErrorType = EventTypes.Critical;
public EventTypes ErrorType
{
get
{
return _ErrorType;
}
set
{
_ErrorType = value;
}
}
public string ErrorStack { get; set; }
public bool ShowMessage { get; set; }
public enum EventTypes
{
Critical,
Warning,
Information
}
public void SendMessage()
{
ErrorEvent newEvent = new ErrorEvent();
newEvent.LogErrorEvent(this);
}
}
在主窗口中,我注册事件处理程序
EventManager.RegisterClassHandler(typeof(ErrorEvent),
ErrorEvent.EventOccuredEvent, new ErrorEvent.ErrorEventEventHandler(LogOccuredErrors));
最后处理它:
private void LogOccuredErrors(object sender, ErrorEvent.RoutedCustomEventArgs e)
{
ErrorEventHelper eventHelper = e.OccuredEventDetails;
//Call Database Helper to log to database or output to file and email
StringBuilder builder = new StringBuilder();
builder.AppendLine(eventHelper.ErrorType);
builder.AppendLine(eventHelper.ErrorValue);
builder.AppendLine(eventHelper.ErrorStack);
if (eventHelper.ShowMessage)
{
MessageBox.Show(eventHelper.ErrorValue);
}
//Create your log file and email it
File.WriteAllText(@"C:\log.txt", ControllerBuilder.ToString())
//email here
}
他的问题是,我引用他的话,“我也发现了log4net,但它仍然以同样的方式工作。”@Yuriy谢谢,错过了他说的地方,他对try-catch的依赖让我觉得他没有看到它。考虑到这一点,显然对Appender如何工作还没有完全了解。正在改进我的答案。谢谢回复。我认为Application.ThreadException可能是我的问题的答案,但我对它没有太多的想法或知识,所以让我阅读整篇文章,如果我有任何进一步的问题,我会给你回复。你不需要把try-catch放在任何地方,也许需要知道关于处理异常的最佳实践;看见
EventManager.RegisterClassHandler(typeof(ErrorEvent),
ErrorEvent.EventOccuredEvent, new ErrorEvent.ErrorEventEventHandler(LogOccuredErrors));
private void LogOccuredErrors(object sender, ErrorEvent.RoutedCustomEventArgs e)
{
ErrorEventHelper eventHelper = e.OccuredEventDetails;
//Call Database Helper to log to database or output to file and email
StringBuilder builder = new StringBuilder();
builder.AppendLine(eventHelper.ErrorType);
builder.AppendLine(eventHelper.ErrorValue);
builder.AppendLine(eventHelper.ErrorStack);
if (eventHelper.ShowMessage)
{
MessageBox.Show(eventHelper.ErrorValue);
}
//Create your log file and email it
File.WriteAllText(@"C:\log.txt", ControllerBuilder.ToString())
//email here
}