有时候在用log4net的时候,调试或执行是ok的,但是安装服务后没有记录日志。
这是因为服务启动是在C盘启动,而程序放的位置在别的目录。
这时候需要指定读取配置文件的位置为程序所在的目录
string execuFilePath = Assembly.GetExecutingAssembly().Location;
string execuDirPath = Path.GetDirectoryName(execuFilePath);
string log4netconfigFilePath = execuDirPath + "\\log4net.config";
//初始化log4net配置的信息
XmlConfigurator.ConfigureAndWatch(log4net.LogManager.GetRepository(Assembly.GetExecutingAssembly()), new FileInfo(log4netconfigFilePath));
//初始化logger
ILog logger = LogManager.GetLogger(typeof(ServerJobRunning));
之后就可以正常使用了。