Common.logging不会在类库中获取App.config

时间:2014-01-21 20:52:57

标签: c# .net app-config common.logging

类库项目有一个App.config文件,如下所示:

<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>

在c#代码中,我有:

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);
}

此类库使用的方式是将其导入PowerShell,从而为其添加自定义cmdlet。当我打开powerShell并执行自定义cmdlet时,open显示Logger被视为默认记录器,它没有做任何事情,但我确实希望有一个合适的记录器来记录文件。

我怀疑由于这是一个类库,它会自动生成一个solutionName.dll.config文件,而common.Logging无法识别该文件,而这可能需要一个exe.config?

0 个答案:

没有答案
相关问题