Common.logging doesn't pick up the App.config in a class library

1.2k Views Asked by At

The class library project has a App.config file as given:

<configuration>
    <configSections>
        <sectionGroup name="common">
            <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    </configSections>
    <common>
        <logging>
            <factoryAdapter type="MyApp.Logging.NLog2FactoryAdapter, MyApp.Logging">
                <arg key="configType" value="INLINE" />
            </factoryAdapter>
        </logging>
    </common>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.netfx40.xsd">
        <extensions>
            <add assembly="MyApp.Logging" />
        </extensions>
        <targets async="true">
            <target type="Console" name="ConsoleTarget" layout="${longdate}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring:innerFormat=tostring:maxInnerExceptionLevel=8}" />
            <target type="File" name="FileTarget" fileName="C:\Users\liangj\Desktop\log.log" layout="${longdate}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring:innerFormat=tostring:maxInnerExceptionLevel=8}" />
            <target type="JsonNetwork" name="SplunkTarget" address="tcp4://log-staging.dbrs.local:10500" newLine="true" layout="${message} ${exception:format=tostring:innerFormat=tostring:maxInnerExceptionLevel=8}" />
        </targets>
        <rules>
            <logger name="*" minlevel="Debug" writeTo="SplunkTarget,ConsoleTarget,FileTarget" />
        </rules>
    </nlog>
    <appSettings>
        <add key="AppVersion" value="2.1" />
    </appSettings>
</configuration>

In the c# code, I have:

public static void WriteLog(string msg, IDictionary<string, object> metadata)
{    
    Configuration Configuration = ConfigurationManager.OpenExeConfiguration(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath);
    string version = Configuration.AppSettings.Settings["AppVersion"].Value.ToString();
    string LogTime = DateTime.Now.ToString("yyyy-M-dd H:mm:ss tt");
    metadata["LogTime"] = LogTime;
    Console.WriteLine(version );
    Console.WriteLine(Logger.Log.ToString());
    Logger.Log.InfoWithMetadata(msg, metadata);
}

The way this class library used is that it will be imported into PowerShell which adds custom cmdlets to it. When I open powerShell and execute the custom cmdlet, the open shows that the Logger is a taken as a default logger which doesn't do anything, but I do expect a proper logger to log to files.

I doubt that since this is a class library, it automatically generates a solutionName.dll.config file, which is not recognized by common.Logging, which may expects a exe.config instead?

0

There are 0 best solutions below