为 log4net 中类的每个实例创建日志的最佳方法是什么?

发布于 2024-11-09 16:27:06 字数 244 浏览 2 评论 0原文

我们有几个长期存在的对象,每个对象都在系统中属于同一类。大约只有 5 或 6 个,它们是与外部系统的连接。我希望每个实例都有自己可以登录的文件。

最好的方法是什么?我现在只能看到以编程方式添加记录器作为答案。

更新: 我想避免使用配置文件,因为如果我向不同的远程主机添加新连接,那么我希望其日志输出转到以连接命名的文件,而不必首先在配置文件中进行修改。如果能自动完成就好了。我不知道这是否可以在配置文件中一次性实现,并且您不需要每次都编辑它。

We have a couple of long lived objects each of the same class in the system. There are only about 5 or 6 and they are connections to outside systems. I want each of those instances to have their own file that they can log to.

What is the best way to do this? I can only see adding loggers programatically as the answer right now.

UPDATE:
I want to avoid using the configuration file because if I add a new connection to a different remote host then i want its log output to go to file named after the connection without having to first hack around in a config file. It would be nice if it was done automagically. I don't know maybe this can be achieved in a config file once off and that you don't need to edit it everytime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤单情人 2024-11-16 16:27:06

最好的答案是编程日志记录,我遇到了同样的问题,并发现它是唯一的解决方案。

如需更多帮助,请检查此问题(我的)
我发布的 StackOverFlow 问题

和我认为你的问题和我的问题是一样的,动态记录器,重新创建记录器,在运行时处理记录器和所有这些东西:)

如果还有任何问题,请询问,我很乐意提供帮助,因为我已经完成了所有这些:)

The best answer IS programatical logging, i went through the same problem and found it as the only solution.

For more help check this question (mine)
StackOverFlow Question posted by me

and i thing yours and my problem are the same, dynamic loggers, re create loggers, dispose loggers and all that stuff at runtime :)

if any more questions, do ask, will be glad to help since i have gone through all that :)

折戟 2024-11-16 16:27:06

我实际上可以看到几种可能性:

  1. 将每个日志记录分配给每个连接的不同类别,并将每个类别的日志附加到不同的文件。
  2. 按照您的建议以编程方式创建新的记录器。
  3. 使用依赖项注入根据需要注入任意数量的单独记录器。

I can actually see a couple of possibilities:

  1. Assign each logging to a different category for each connection and append logs for each category to different files.
  2. Create new loggers programmatically as you suggested.
  3. Use dependency injection to inject as many separate logger as you need.
孤独难免 2024-11-16 16:27:06

我有一篇文章可能会有所帮助:

http://horth.com/blog/?p=165

这是关于在运行时更改日志文件。您可以做的是将每个实例的文件名传递到 log4net 文件中。这样您就可以为类的每个实例创建一个日志文件。

至于您不想使用配置文件的编辑,此方法可以解决该问题,以便您可以使用配置文件,但仍然具有所需的灵活性。

I have an article that might help:

http://horth.com/blog/?p=165

This is about changing a logfile at runtime. What you could do is pass in the file name for each instance into your log4net file. That way you could create a log file for each instance of your class.

As for your edit about not wanting to use a config file, this method gets around that issue so that you can use a config file but still have the flexibility you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文