0

我正在做一些不寻常的事情。

我有一个应用程序,它作为 Windows 服务运行。

它的作用是,它监视一个文件夹,当有一些新文件放入该文件夹时,应用程序将对文件执行某些操作。每当处理一个文件时出现错误。我需要创建一个文本文件,并将错误/异常信息放入该文本文件中。(以后我可以用这个文件做点什么)

所以有这样的事情

FileWatch, when there is a new file, do following :
     try
     {
         processing file
     }
     catch(Exception ex)
     {
         MyLogger write exception message into one new text file
     }

到目前为止,我是如何做到的。我创建了一个类,例如 MyLogger,每当我新建一个 MyLogger 时,它都会创建一个文本文件(名称很重要,需要是特定格式),并且在 MyLogger“WriteError(字符串消息)”中有一个方法,它写道文本到该文件中。

因为我在我的应用程序中使用了 log4net。你认为我应该修改我的记录器,从 log4net 扩展一些类,这样我就可以获得一些好处吗?(不知道我会得到什么样的好处,但 log4net 是一个很好的日志框架,它处理文本文件的方式可能有我不知道的东西)

谢谢

4

5 回答 5

2

log4net or any other generic logger is helpful if
1) you want to have a consistent logging facility in many places across your application; and/or
2) you want the ability to customize logging format, level and so on.

From your description it sounds like there is a single point in your app where you need to log the exception in a specific way. If this is correct, you will probably gain no benefit from creating a custom logger - just write a method that logs exception to a file in the way you need.

If I misunderstood you, and there is a need for generic logger (that is, either 1) or 2) above is true), extending log4net by inheriting a logger or creating a wrapper is fine.

于 2010-11-03T10:37:23.080 回答
1

我之前创建了 log4net 包装器。我发现以这种方式开始很方便,因为您并不总是知道项目开始时的日志记录要求。我的规则是 log4net 库只能从我自己的“日志”命名空间中引用。这样,应用程序代码只调用包装器,而包装器是与 log4net 功能的唯一联系点。

从长远来看,投资构建自己的记录器可能是值得的。如果您正确封装 log4net,您应该能够相当轻松地进行此升级,而无需更改您的代码。

于 2010-11-03T05:31:30.327 回答
0

我不确定我是否会为此目的使用日志框架。我的印象是,在异常情况下编写此文本文件是您业务流程的一部分。日志记录有不同的用途,可以在不影响业务流程的情况下关闭...

于 2010-11-03T10:25:43.053 回答
0

为什么不使用.NET 框架中的跟踪侦听器?它们提供了日志网络的许多好处,而无需合并外部框架。

好处包括集中式日志管理以及将输出日志定向到一个或多个源的能力,例如控制台窗口、文本文件或 Windows 事件日志。

于 2010-11-03T04:49:39.303 回答
0

您应该花一些时间创建自己的记录器,它完全符合您的要求。这将是最好的方法。也相当简单,您可以完全控制自定义,因此您可以使输出看起来和感觉就像在 log4net 中一样。您可以谷歌记录示例并开始修改该示例。

于 2010-11-03T05:12:19.693 回答