创建自定义记录器,我应该从 log4net 扩展类吗
我正在做一些不寻常的事情。
我有一个应用程序,它作为 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 是一个很好的日志框架,它处理文本文件的方式可能有我不知道的事情)
谢谢
I am doing something unusual.
I have an application, it runs as a windows service.
what it does is that, it monitor one folder, when ever there is some new file put into that folder, the application will do something to the file.
Whenever there is an error when processing one file. I need to create a text file, and put the error/exception information into that text file. (later i can do something with this file)
so there is something like this
FileWatch, when there is a new file, do following :
try
{
processing file
}
catch(Exception ex)
{
MyLogger write exception message into one new text file
}
So far how i did it is that. I create a class for example MyLogger, whenever i new one MyLogger, it creates a text file (the name matters, need to be a specific format), and there is one method in side MyLogger "WriteError(string message)", it writes text into that file.
Since i used log4net in my application. Do you think i should modify my logger, to extend some class from log4net, so that i can get some benefit? (not sure what kind of benefit i will get, but log4net is a good logging framework, the way it handle text file might have thing that i do not aware)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
log4net 或任何其他通用记录器在以下情况下很有帮助
1) 您希望在应用程序的许多地方都有一致的日志记录工具;和/或
2)您希望能够自定义日志记录格式、级别等。
从您的描述来看,您的应用程序中似乎有一个点需要以特定方式记录异常。如果这是正确的,您可能不会从创建自定义记录器中获得任何好处 - 只需编写一个以您需要的方式将异常记录到文件的方法。
如果我误解了你的意思,并且需要通用记录器(即,上面的 1)或 2)是正确的),那么通过继承记录器或创建包装器来扩展 log4net 就可以了。
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.
我之前创建过 log4net 包装器。我发现以这种方式开始很方便,因为您并不总是知道项目开始时的日志记录要求是什么。我的规则是 log4net 库只能从我自己的“logging”命名空间引用。这样,应用程序代码仅调用包装器,而包装器是 log4net 功能的唯一联系点。
从长远来看,投资构建自己的记录器可能是值得的。如果正确封装 log4net,您应该能够相当轻松地进行此升级,而无需更改代码。
I've created log4net wrappers before. I find it handy to start this way as you don't always know what the logging requirements are at the start of a project. My rule has been that the log4net library can only be referenced from my own "logging" namespace. This way, the application code only calls the wrapper, and the wrapper is the only point of contact to the log4net functionality.
In the long run, it's probably worth investing in building your own logger. If you encapsulate log4net properly, you should be able to make this upgrade rather easily, without having to change your code.
为什么不使用 .NET 框架中的跟踪侦听器?它们提供了日志网络的许多优点,而无需合并外部框架。
优点包括集中日志管理以及将输出日志定向到一个或多个源(例如控制台窗口、文本文件或 Windows 事件日志)的能力。
Why not use Trace Listeners from the .NET framework? They provide many of the benefits of a logging network, without the need to incorporate an external framework.
Benefits include centralized log management and the ability to direct the output logs to one or more sources such as a console window, text file, or the Windows Event Log.
您应该花一些时间创建自己的记录器,它完全可以满足您的需求。这将是最好的办法。也相当简单,您可以完全控制自定义,因此您可以使输出的外观和感觉与 log4net 中一样。您可以通过 Google 搜索日志示例并开始修改该示例。
You should spend some time creating your own logger that does exactly what you want. This would be the best way. Is also fairly easy and you have full control on the customization so you can make the output look and feel as in log4net. You could Google for logging sample and start modifying that one.
我不确定是否会为此目的使用日志框架。我的印象是,在异常情况下编写此文本文件是您业务流程的一部分。日志记录有不同的用途,可以在不影响业务流程的情况下关闭它......
I am not sure if I would use a log framework for this purpose. I have the impression that writing this text file in the exception case is part of your business process. Logging serves a different purpose that can be turned off without affecting business processes...