我需要帮助将错误写入单独的文本文件并将该文件通过电子邮件发送给我。实际上,我创建了Windows应用程序,但我没有添加任何异常或将错误写入单独的文件。现在,我希望每当我的应用程序因任何错误而中断时,应将错误记录到单独的文本文件中,该文件应该通过电子邮件发送给我。
我对错误日志文件进行了研究,我发现创建了单独的类,用于将错误日志写入文本文件,并将该文件调用到所有方法的try-catch块中。但我的程序/应用程序已经变得庞大,我需要添加它的中间体。另外,我发现了log4net,但它仍然以相同的方式工作。
任何人都可以指导我如何在应用程序级别或应用程序收到任何错误消息时编写错误日志吗?
感谢您的帮助。
答案 0 :(得分:1)
如果您还没有加入任何系统,我建议您查看log4net,因为它会按照您的要求,方式,方式,更多来做,并且是一个受欢迎的图书馆,所以它将由您团队中的其他专业人员识别和扩展。此外,由于其他人维护它,您偶尔会得到免费的错误修复。
答案 1 :(得分:0)
据我所知,您所需要的只是处理AppDomain.UnhandledException Event。它将为您提供有关崩溃的最常见信息。您可以使用它来开始研究崩溃原因。
当然,在UnhandledException
处理程序中,您可以使用任何您想要的内容,log4net
或自定义记录器。
答案 2 :(得分:0)
试
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="to@domain.com" />
<from value="from@domain.com" />
<subject value="test logging message" />
<smtpHost value="SMTPServer.domain.com" />
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
答案 3 :(得分:0)
要处理未在其他地方捕获的所有异常,请考虑为Application.ThreadException
实现处理程序答案 4 :(得分:0)
我创建了一个用于WPF项目的日志助手。这是一个包含许多不同组件的大型项目。这是在我学习如何使用MVVM之前所以请原谅这是否打破了模式。该系统不断扩展,使用新组件来处理不同的任务。您可以将组件视为处理特定任务的独立单元,主组件是其中的主要组件。此日志记录允许我们记录错误,无论它们发生在哪个组件中。这可能是一种矫枉过正,但对我们来说效果很好。
我使用相同的原则来记录所有系统范围的事件(用户操作,数据更改......等)
在我的App.xaml.cs中,我捕获了所有未处理的错误。请注意,这是记录的最后手段,我使用try catch,我希望发生异常(例如db连接)。
public partial class App : Application
{
void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
ErrorEvent errorEvent = new ErrorEvent();
errorEvent.LogErrorEvent(e.Exception);
e.Handled = true;
}
}
ErrorEvent是我的Utilities dll中的一个类,它使用事件路由记录错误。我有不同类型的错误,具体取决于严重程度。所有这些错误都记录在数据库中,唯一知道数据库连接的组件是主要的。这就是我使用Routing Event将异常发送到要记录的主要组件的原因。 ErrorEventHelper将填充并路由到主组件。在我使用try catches的情况下,我创建了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);
}
}
在MainWindow中,我注册了事件处理程序。
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
}