简体   繁体   English

来自 app.config 中的键的 Log4net 配置文件路径

[英]Log4net configuration file path from a key in app.config

I have a created a custom log component class library which uses log4net.我创建了一个使用 log4net 的自定义日志组件 class 库。 I want to have the log4net configuration file path to be read from a key in app.config.我想从 app.config 中的键读取 log4net 配置文件路径。 so app.config will have a key "log4netconfigpath" and this key will contain path of configuration file.所以 app.config 将有一个键“log4netconfigpath”,这个键将包含配置文件的路径。 So how I load log4net configuration from a key in app.config.那么我如何从 app.config 中的一个键加载 log4net 配置。

Currently I read config from a fixed path like below.目前我从如下固定路径读取配置。

[assembly: log4net.Config.XmlConfigurator(ConfigFile=@"log4netconfig.config", Watch=true)]

I think you use the below code while initializing your application,我认为您在初始化应用程序时使用以下代码,

var logPath = Convert.ToString(ConfigurationManager.AppSettings["log4netconfigpath"])
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(path));

But if you are using this in the Console or Winservice app and adding the log config file inside the project then please get the basePath and then append the path但是,如果您在控制台或 Winservice 应用程序中使用它并在项目中添加日志配置文件,那么请获取 basePath,然后获取 append 路径

string path = System.AppDomain.CurrentDomain.BaseDirectory;
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(path + logPath)));

Namespace: System.Configuration命名空间:System.Configuration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM