How to configure log4net for fallback

自闭症网瘾萝莉.ら 提交于 2019-12-10 18:37:12

问题


This is my situation. I have successfully implemented logging to remote syslog using log4net. However, as far as I could test, if syslog IP is not valid, all messages will not log anywhere and no exception is raised. It just does nothing.

Hence, it would be nice to have some sort of fallback. Let's say if writing to syslog fails, write to file or to database.

Is that even possible with log4net? Or would I have to configure it to log to two locations at the same time?


回答1:


I don't think you can do this by configuration. This issue is open in the log4net feature backlog.

If your application can eat the logging overhead, the easiest solution would be to log to an alternative appender by default.

Alternatively you could try to wrap the appender you're using in a custom appender, and implement the fallback scenario if the syslog appender throws an exception. If it doesn't swallow them silently.

From log4net FAQ:

You can implement the log4net.Appender.IAppender interface to create you own customized appender. We recommend that you extend the log4net.Appender.AppenderSkeleton class rather than starting from scratch. You should implement your custom code in a assembly separate from the log4net assembly.

To get started it is worth looking at the source of the log4net.Appender.TraceAppender as an example of the minimum amount of code required to get an appender working.

Third option would be to look into the source code of your appender and see if you can fork it and do the necessary customizations there.



来源:https://stackoverflow.com/questions/3244623/how-to-configure-log4net-for-fallback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!