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

279 Views Asked by At

I have 12 projects in my solution file. There are most Windows services (ServiceProj_1, ServiceProj_2, ...) and one project is of web application (WebApp). I use log4net for logging. WebApp and ServiceProject_1, ServiceProj_2, ... have log4net configuration into web.config and app.config files respectively. We have implemented a DMZ, so the WebApp is only exposed to the other people. Now there is a requirement to use logging of those windows service projects instead of WebApp.

I have come to know that I can create a custom appender and make it possible. 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.

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.

1

There are 1 best solutions below

0
AntDC On

You can specify the config file and load it dynamically... Here my config file is found at the location of 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.