简体   繁体   English

C# - 如何在当前项目中使用另一个项目日志配置?

[英]C# - How to use another project logging configuration into current project?

I have 12 projects in my solution file.我的解决方案文件中有 12 个项目。 There are most Windows services ( ServiceProj_1, ServiceProj_2, ... ) and one project is of web application ( WebApp ).大多数 Windows 服务( ServiceProj_1, ServiceProj_2, ... )和一个项目是 Web 应用程序( WebApp )。 I use log4net for logging.我使用 log4net 进行日志记录。 WebApp and ServiceProject_1, ServiceProj_2, ... have log4net configuration into web.config and app.config files respectively. WebAppServiceProject_1, ServiceProj_2, ...将 log4net 配置到web.configapp.config文件中。 We have implemented a DMZ, so the WebApp is only exposed to the other people.我们已经实现了一个 DMZ,所以WebApp只暴露给其他人。 Now there is a requirement to use logging of those windows service projects instead of WebApp .现在需要使用这些 windows 服务项目的日志记录而不是WebApp

I have come to know that I can create a custom appender and make it possible.我开始知道我可以创建自定义 appender 并使其成为可能。 The catch is, there are lots of lines already written into WebApp to log a LogMessage into log file so we cannot touch those lines.问题是,有很多行已经写入WebApp以将LogMessage记录到日志文件中,因此我们无法触及这些行。

I have no idea what to do and how to do.我不知道该做什么以及如何做。 Need help.需要帮忙。 If the description is not understandable then please let me know I will try to explain more.如果描述无法理解,请告诉我,我会尽力解释更多。

You can specify the config file and load it dynamically... Here my config file is found at the location of FullConfigFilePath.您可以指定配置文件并动态加载它...这里我的配置文件位于 FullConfigFilePath 的位置。

private Configuration Config
{
    get
    {
        if (_Config != null) return _Config;

        _Config = ConfigurationManager.OpenMappedExeConfiguration(
        new ExeConfigurationFileMap()
        {
            ExeConfigFilename = FullConfigFilePath
        }, ConfigurationUserLevel.None);
        return _Config;
    }
}   

Once the config is loaded you can access the values from there.... For instance.....加载配置后,您可以从那里访问值......例如......

private string BaseUrl
{
    get
    {
        return this.Config.AppSettings.Settings["MyConfigSetting"].Value;
    }
}

Hopefully you can tweak and use this sort of approach for your needs.希望您可以根据需要调整和使用这种方法。

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

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