如何通过代码启用/禁用存储在配置中的日志记录和文件附加程序信息?
我有一个使用 log4net 进行日志记录的框架,我知道我们可以通过配置文件禁用或启用 log4net 中所需的日志记录。我想开发一些设置类,以便管理员或用户可以设置日志记录等。log4net 中的类等是什么来获取配置文件并通过代码更改配置文件中的设置? (有没有或者我应该使用.net System.Xml)有任何代码片段吗?
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="ALL" />
<!--<appender-ref ref="LogFileAppender" />-->
<!--<appender-ref ref="ConsoleAppender" />-->
<appender-ref ref="DEBUG-RollingLogFileAppender" />
<appender-ref ref="Error-RollingLogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n"/>
<param name="Footer" value="[Footer]\r\n"/>
<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />
</layout>
</appender>
<appender name="DEBUG-RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="..\\logs\\debug" />
<param name="AppendToFile" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value=".yyyyMMdd'.log'" />
<param name="MaxSizeRollBackups" value="30" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout,log4net">
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />-->
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<!--<levelMin value="INFO" />-->
<levelMax value="INFO" />
</filter>
</appender>
<appender name="Error-RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="Threshold" value="WARN"/>
<param name="File" value="..\\logs\\errors" />
<param name="AppendToFile" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value=".yyyyMMdd'.log'" />
<param name="MaxSizeRollBackups" value="30" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout,log4net">
<header value=" " />
<footer value=" ======================================================================" />
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
</log4net>
I have a framework that uses log4net for logging, I know that we can disable or enable the desired logging in log4net via the config file. I want to develop some settings class so that the admin or user can set the logging etc.. What is the class etc in log4net to get the config file and change the settings in the config file via code? (are there any or should I use .net System.Xml) Any code snippets?
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="ALL" />
<!--<appender-ref ref="LogFileAppender" />-->
<!--<appender-ref ref="ConsoleAppender" />-->
<appender-ref ref="DEBUG-RollingLogFileAppender" />
<appender-ref ref="Error-RollingLogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n"/>
<param name="Footer" value="[Footer]\r\n"/>
<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />
</layout>
</appender>
<appender name="DEBUG-RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="..\\logs\\debug" />
<param name="AppendToFile" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value=".yyyyMMdd'.log'" />
<param name="MaxSizeRollBackups" value="30" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout,log4net">
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />-->
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<!--<levelMin value="INFO" />-->
<levelMax value="INFO" />
</filter>
</appender>
<appender name="Error-RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="Threshold" value="WARN"/>
<param name="File" value="..\\logs\\errors" />
<param name="AppendToFile" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value=".yyyyMMdd'.log'" />
<param name="MaxSizeRollBackups" value="30" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout,log4net">
<header value="
" />
<footer value="
======================================================================" />
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
</log4net>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题不太清楚:您是否尝试以编程方式更改当前运行的配置,或者以编程方式更改配置文件?
如果是后者,我认为您不需要 log4net 本身的任何东西 - 而且我不会特别期望它提供一个 API 来操作文件。相反,我会使用 LINQ to XML(假设是 .NET 3.5 或更高版本)并自己直接操作它。从外观上看,这很简单。研究配置文件格式,找出您想要对其进行哪些更改,然后为其编写适当的代码。您甚至不需要为此引用 log4net。
Your question isn't exactly clear: are you trying to change the currently-running configuration programmatically, or change the config file programmatically?
If it's the latter, I don't think you need anything from log4net itself - and I wouldn't particularly expect it to provide an API to manipulate the file. Instead, I'd use LINQ to XML (assuming .NET 3.5 or higher) and manipulate it myself directly. It's pretty simple, by the looks of it. Study the configuration file format, work out what changes you'd want to make to it, then write the appropriate code for it. You shouldn't even need to reference log4net for this.
看来你可以使用Peter的答案并添加或删除附加程序
创建所有附加程序在网络配置中,在以下行之后:
迭代:
删除:
添加:
it seems you can use Peter's answer and add or remove appender
Create all the appenders in the web config and after the line:
Iterate:
Remove:
Add: