应用程序日志 [英] Application logging

查看:117
本文介绍了应用程序日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms应用程序,我需要记录所有的异常(一个文件,一个web服务,等等),无论是处理和未处理。我怎样才能做到这一点?

I have a WinForms application and I need to log all exceptions (to a file, to a webservice, whatever), either handled and unhandled. How can I do this?

推荐答案

在.NET世界中最常用的日志框架是log4net的。

The most commonly used logging framework in the .NET world is log4net.

有这需要在应用程序中集成log4net的两个步骤:

There are two steps that are required to integrate log4net in your application:

  1. 配置log4.net。例如,检查出FileAppender的log4net的配置示例页面。
  2. prepare你的类使用log4net的:

每个类应包含用于记录的字段:

Each class should contain a field used for logging:

private static ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

现在,每当你必须处理异常,你会做以下内容:

Now, whenever you'll have to handle an exception, you would do the following:

try
{
  // Do stuff here
}
catch (Exception ex)
{
  _logger.Error("Operation failed.", ex);
}

在这种方式,你已经解决了一半的问题:处理的异常

为了捕捉所有的未处理的异常在Windows窗体应用程序,你必须处理由.NET框架提供2事件:

In this way you've solved only half of the problem: handled exceptions.

In order to catch all unhandled exceptions in a Windows Forms application, you'll have to handle 2 events provided by .NET framework:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

    Application.ThreadException += Application_ThreadException;
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    _logger.Error("Unhandled exception.", (Exception)e.ExceptionObject);
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    _logger.Error("Unhandled application exception.", e.Exception);
}

这是all.Nice,方便!

事情变得复杂,当你开始交付您的应用程序到一个或多个客户。这是因为你需要问你的客户提供你的日志文件,以检查应用程序是否遇到过任何问题。

因此,你必须解决两个问题:

That's all.Nice and easy!

Things get complicated when you start to deliver your application to one or more customers. This is because you need to ask your customers to provide you the log files in order to check whether the application encountered any issue.

Therefore, you have to solve two problems:

  1. 如何获得通知时,一些错误在应用程序中会发生什么?
  2. 如何获得有关异常,环境等的详细信息?

  1. How to get notified when something wrong happens in your application?
  2. How to get detailed information about the exception, environment, etc. ?

一个解决这两个问题,将使用专用的服务,它集中了所有的异常在一个地方,并通知您,只要您的应用程序遇到任何异常。

A solution to both problems would be to use a dedicated service that centralizes all your exceptions in one single place and that notifies you whenever your application encounters any exception.


有几种服务,试图解决所有这些问题,其中之一就是 ExceptionTail
ExceptionTail的一个非​​常好的特性是它与使用log4.net通过的自定义log4net的Appender的
这意味着您不必重新编译整个应用程序才能使用该服务。刚落,在您的应用程序文件夹某些DLL,添加了一些应用程序配置文件,你是好去。


There are several services that try to solve all the these problems, one of them being ExceptionTail.
A very nice feature of ExceptionTail is its seamless integration with existing applications that use log4.net via a custom log4net appender.
This means that you don't have to recompile your entire application in order to use the service. Just drop some dlls in your application's folder, add something to the application configuration file, and you're good to go.

这篇关于应用程序日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆