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 にインポートされることです。PowerShellを開いてカスタムコマンドレットを実行すると、ロガーが何もしないデフォルトのロガーとして使用されていることがわかりますが、適切なロガーがファイルにログを記録することを期待しています。

これはクラス ライブラリであるため、solutionName.dll.config ファイルが自動的に生成されるとは思えませんが、これは common.Logging によって認識されず、代わりに exe.config が必要になる可能性があります。

4

0 に答える 0