Log4net 在 C# 中无法在多线程中工作

问题描述

你好朋友,我添加了 log4net 包形式的 nuget 包。对于同步调用 log4net 在日志文件添加日志但是当我使用 Task 声明方法时它不起作用

<log4net>
    <appender name="TestAppender" type="log4net.Appender.RollingFileAppender">
      <file value="D:\log\logswebapi.log" />
      <encoding value="utf-8" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <!--<conversionPattern value="%date %level [%thread] %type.%method - %message%n" />-->
      
      </layout>
    </appender>
    
    <root>
      <level value="All" />
      <!-- If the following line is not included the log file 
      will not be created even if log4net is configured with this file. -->
      <appender-ref ref="TestAppender" />
    </root>
  </log4net>


我已将此代码添加到 web.config 文件

 private void AddLog(string str)
        {
            ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            Log.Info(DateTime.Now + " - " + str);
}

 public  string SendDocument(){
// send document logic
AddLog(send document done");
}

public string Response(){
Task.Run(() =>SendDocument()); // here i am calling this senddocument method in thread 
}

在上面的代码响应方法中,我在线程中调用发送文档方法但现在没有添加日志如何解决这个问题?

提前致谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)