log4 net 动态文件名分配
我想知道如何使用 Log4net 动态分配文件名。我的应用程序是这样的:应根据用户输入动态创建 10 个不同的文件,然后根据名称选择相应的文件名并将信息写入其中
例如,在我的应用程序中,根据我对每个 xml 文件的业务要求,应创建与 xml 文件同名的相应日志文件。以后,每当我对 xml 文件进行任何修改时,都需要在相应的日志文件中添加一个
条目帮助 。 我无法控制适当的日志来写入它
I want to know how to dynamically assign file name using Log4net .My application is such that 10 different files should be dynamically created based on user input ,and later based on the name the corresponding file name needs to be picked up and information written to it
For example in my application based on my buisness requirement for every xml file a corresponding log file with the same name as xml file should be created .Later whenever I do any modification to the xml file an entry needs to be in the corresponding log file
Please help . I having trouble to get control of the appropriate log to write it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还没有这样做,但可能有很多方法可以做到这一点,所以这可能不是最好的方法,但它应该可以工作
然后当你需要切换文件时调用 OpenLogfile 即可。
您可能需要调整布局或附加器类型。
此方法的一大缺点是您会丢失 xml 配置以及在运行时更改设置的能力。 因此,更好的方法可能是在 xml 文件中配置您的附加程序以使用属性
,例如
然后在您的代码中您可以更改该属性
棘手的一点是让 log4net 重新加载自身。 我还没有阅读过这方面的文档,所以我不知道是否有办法强制重新加载。 如果您再次调用 log4net.Config.XmlConfigurator.ConfigureAndWatch ,它可能会起作用。 否则,如果您打开 xml 文件并再次保存(无需更改任何内容),它应该可以工作
希望这会有所帮助。
Have not done this, but there are probably a number of ways of doing this, so this may not be the best way, but it should work
Then just call OpenLogfile when you need to switch files.
You might need to tweak the layout or appender type.
A big disadvantage of this method is you losing the xml configuration and the ability to change settings at runtime. So a better way might be to configure your appender in the xml file to use a property
eg
Then in your code you could change the property
The tricky bit is to get log4net to reload itself. I haven't read the documentation of this, so I don't know if there is a way of forcing a reload. It might work if you just call log4net.Config.XmlConfigurator.ConfigureAndWatch again. Otherwise it should work if you opened the xml file and saved it again (without needing to change anything)
Hope this helps.