0

类库项目有一个 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 时,打开显示 Logger 被视为默认记录器,它不执行任何操作,但我确实希望有一个适当的记录器来记录文件。

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

4

0 回答 0