1、在WinForm应用程序中,需要显示调用以下语句: log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(System.Windows.Forms.Application.StartupPath + "\\log4net.config")); 2、Web应用程序,也可以通过调用初始化语句完成: log4net.Config.XmlConfigurator.ConfigureAndWatch(new 中添加设置,如下所示: void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 log4net.Config.XmlConfigurator.ConfigureAndWatch 封装了日志的相关功能,如下: public class Logger { public static void Init(string ConfigFile) { log4net.Config.XmlConfigurator.ConfigureAndWatch System.IO.FileInfo(ConfigFile)); } public static void WinFormInit() //WinForm程序的初始化 { log4net.Config.XmlConfigurator.ConfigureAndWatch
修改方法:在log4net.config上右击-->属性--->把"复制到输出目录" 值改为true; 3.在要用到log4的地方命名空间上边加上:[assembly: log4net.Config.XmlConfigurator RollingLogFileAppender" /> </root> </log4net> </configuration> 在使用Log4net的地方添加: [assembly: log4net.Config.XmlConfigurator 修改方法:在log4net.config上右击-->属性--->把"复制到输出目录" 值改为true; 3.在要用到log4的地方命名空间上边加上:[assembly: log4net.Config.XmlConfigurator </log4net> </configuration> 在使用Log4net的地方添加: [csharp] view plaincopy [assembly: log4net.Config.XmlConfigurator
Log4net 1.引用dll 2.添加log4net.config,设置“始终复制” 4.assemblyinfo.cs中添加 [assembly: log4net.Config.XmlConfigurator console中输出,必须要把程序的输出设置为控制台输出 asp.net的配置方法 1.添加global.ascx,在Application_Start中添加 log4net.Config.XmlConfigurator.ConfigureAndWatch
ConfigurationManager.AppSettings["log4net"]; var fi = new System.IO.FileInfo(path); log4net.Config.XmlConfigurator.Configure ConfigurationManager.AppSettings["log4net"]; var fi = new System.IO.FileInfo(path); log4net.Config.XmlConfigurator.Configure (fi); 上面fi就是log4net.config的文件位置,然后 log4net.Config.XmlConfigurator.Configure(fi); 然后就是LogHelper类了,这个是个简单的日记帮助类
using Topshelf; namespace SampleWindowsService { class Program { static void Main(string[] args) { XmlConfigurator.ConfigureAndWatch s.SetServiceName("SampleService"); s.ConstructUsing(name => new SampleService()); s.WhenStarted(tc => { XmlConfigurator.ConfigureAndWatch
System.AppDomain.CurrentDomain.BaseDirectory, "Log4net.config"); ICollection c = log4net.Config.XmlConfigurator.ConfigureAndWatch </layout> </appender> </log4net> </configuration> Main函数中: ICollection c = log4net.Config.XmlConfigurator.ConfigureAndWatch ICollection c = log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(path)); 这行代码返回值是
internal class Program { static void Main(string[] args) { XmlConfigurator.ConfigureAndWatch var instanceName = ConfigurationManager.AppSettings["WindowsServiceInstanceName"]; XmlConfigurator.ConfigureAndWatch
System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); XmlConfigurator.ConfigureAndWatch
logConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log4Net.config"); XmlConfigurator.Configure
下面的代码会根据配置信息来初始化一个Repository,一般会在程序启动的时候率先完成调用: log4net.Config.XmlConfigurator.Configure(); 配置好log4net 的信息,并调用XmlConfigurator.Configure()之后,就可以开始记日记了: log4net.LogManager.GetLogger().Info("Hello world!") 调用XmlConfigurator.Configure()之后,这些配置信息会在Repository里初始化好。 name="LevelMin" value="DEBUG"/> <param name="LevelMax" value="DEBUG"/> </filter> </appender> 执行XmlConfigurator.Configure
layout> </appender> </log4net> 在AssemblyInfo.cs:配置文件中读取配置Log4net.dll [assembly: log4net.Config.XmlConfigurator
假定程序为MyDemo.exe,则需要一个MyDemo.exe.config文件) //如果是WebForm,则从web.config中读取相关信息 [assembly: log4net.Config.XmlConfigurator 在这里需要特别说明一下,注意上面的代码中有这么一句:[assembly: log4net.Config.XmlConfigurator(Watch = true)](在需要使用log4net的类的namespace
--[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config AddEnvironmentVariables(); repository = LogManager.CreateRepository("NETCoreRepository"); XmlConfigurator.Configure
用于解析配置文件: public interface Configurator { void doConfigure(); } 再创建一个默认的XML形式的配置解析器: public class XMLConfigurator implements Configurator{ private final LoggerContext loggerContext; public XMLConfigurator Configurator configurator = null; if(urlString.endsWith("xml")){ configurator = new XMLConfigurator
} } 六、总结 现在回头看《.Net魔法堂:log4net详解》当中的那句 [assembly:log4net.Config.XmlConfigurator
class Program { static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure
logfatal = LogManager.GetLogger("Fatal"); static LogHelper() { log4net.Config.XmlConfigurator.Configure
%m, %message :表示日志的具体内容 %n, %newline: 换行 在AssemblyInfo.cs中添加如下代码: [assembly: log4net.Config.XmlConfigurator protected void Application_Start(object sender, EventArgs e) 2 { 3 log4net.Config.XmlConfigurator.Configure
3.建立globe.asax文件,在Application_Start中加入 log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo
--> <sectionGroup name="fluorinefx"> <section name="settings" type="FluorineFx.Configuration.<em>XmlConfigurator</em>