dll 不同实例的通用 log4net 日志记录
我在配置 log4net 以按我想要的方式工作时遇到问题。我有一个类库 Library.dll,我的应用程序 Mine.exe 和第三方应用程序 Other.exe 都使用它。我有另一个类库 Util.dll,Mine.exe 和 Library.dll 都使用它。
Mine.exe 和 Other.exe 并行运行,我希望 Library.dll 的两个实例使用相同的日志文件。它还应该与 Mine.exe 的日志文件分开。从 Library.dll 调用时,Util.dll 应记录到 Library.dll 日志文件;从 Mine.exe 调用时,应记录到 Mine.exe 日志文件。
编辑:我想这有点难以理解,这就是我希望我工作的方式:
Mine.exe 记录到 Mine.log
Other.exe 不记录任何内容(第三方应用程序)
我的.exe -> Library.dll 记录到 C:\Library.log
其他.exe -> Library.dll 记录到 C:\Library.log
我的.exe -> Util.dll 记录到 Mine.log
我的.exe ->库.dll -> Util.dll 记录到 C:\Library.log
其他.exe ->库.dll -> Util.dll 日志记录到 C:\Library.log Other.exe 不直接调用 Util.dll。
首先,我尝试在 Library.dll 中加载自定义 log4net 配置,使用:
log4net.Config.XmlConfigurator.Configure(new FileInfo("Library.log4net.config"));
但这导致 Mine.exe 也记录到 Library.dll 日志文件。
然后,我尝试在 Library.dll 中添加以下程序集属性:
[assembly: log4net.Config.Repository("Library")]
这使日志保持独立,但即使从 Library.dll 调用,Util.dll 也会记录到 Mine.exe 日志文件中。我想我可以在 Util.dll 中使用 Repository("Util"),在 Library.dll 中使用 AliasReposity("Util", "Library") ,在 Mine.exe 中使用 AliasReposity("Util", "Mine") 但实际上我有很多项目中的类库,并且不想走这条路。
关于如何让它发挥作用有什么想法吗?
/安德烈亚斯
I'm having trouble configuring log4net to work the way I want. I have a class library Library.dll that is used both by my application Mine.exe and a third party aplication Other.exe. I have another class library Util.dll that is used by both Mine.exe and Library.dll.
Mine.exe and Other.exe are run in parallel and I want the two instances of Library.dll to use the same logfile. It should also be separate from the logfile of Mine.exe. Util.dll should log to Library.dll logfile when called from Library.dll, and the Mine.exe logfile when called from Mine.exe.
Edit: I guess that was a bit hard to follow, this is how I want i to work:
Mine.exe logs to Mine.log
Other.exe doesn't log anything (third party application)
Mine.exe -> Library.dll logs to C:\Library.log
Other.exe -> Library.dll logs to C:\Library.log
Mine.exe -> Util.dll logs to Mine.log
Mine.exe -> Library.dll -> Util.dll logs to C:\Library.log
Other.exe -> Library.dll -> Util.dll logs to C:\Library.log
Other.exe doesn't call Util.dll directly.
First I tried loading a custom log4net configuration in Library.dll, using:
log4net.Config.XmlConfigurator.Configure(new FileInfo("Library.log4net.config"));
But that resulted in Mine.exe also logging to the Library.dll logfile.
I then tried adding the following assembly attribute in Library.dll:
[assembly: log4net.Config.Repository("Library")]
That kept the logs separate, but then Util.dll logged to the Mine.exe logfile even when called from Library.dll. I guess I could use Repository("Util") in Util.dll, AliasReposity("Util", "Library") in Library.dll and AliasReposity("Util", "Mine") in Mine.exe but I actually have many class libraries in the project and would rather not go down that route.
Any ideas on how to get this working?
/Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
老实说,我不会共享日志文件。如果您从两个进程登录到文件,则可能会遇到各种线程问题。通常,如果另一个 log4net 实例能够记录,则一个 log4net 实例将无法记录,因此会导致日志条目丢失。此外,如果它们位于同一个日志文件中,您需要将进程名称放入日志格式化程序中,以便您知道来源。听起来很复杂。按照惯例,我通常会执行以下操作: 在
您将获得两个进程的两个文件,但这更易于管理。您可以获得可以一起显示两个日志的日志文件观察程序(尽管我倾向于不使用这种方法),例如: http:// /tailforwin32.sourceforge.net/
I would not share log files to be honest. There are all sorts of threading problems which you may encounter if you log to a file from two processes. Typically one log4net instance will just not be able to log if the other one is, so it will cause log entries to go missing. Also if they are in the same log file, you need to chuck the process name in the log formatter so you know the source. Sounds complicated. By convention, I usually do the following:
You get two files for two processes but this is easier to manage. You can get log file watchers which can display both logs together (although I tend not to use that approach) such as: http://tailforwin32.sourceforge.net/