0

hello friends i have added log4net package form nuget packages. for sync call log4net add logs in log file but when i am declaring a methods using Task its not working

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


i have added this code in web.config file

 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 
}

in above code Response method i am calling send document methods in thread but logs are not added now how to resolve this issue ?

thanks in advance

ketan
  • 15
  • 5
  • Maybe [this](https://stackoverflow.com/questions/7044497/how-do-i-create-an-asynchronous-wrapper-for-log4net) will help? – Trevor Apr 08 '21 at 13:13
  • What happens when you assign the task to a variable then wait for the task on the next line? `Task t = Task.Run(() =>SendDocument()); t.Wait();` – Dumisani Apr 08 '21 at 13:24
  • @Dumisani log added but user need to wait for Response reply. i want to async the Response method. i dont want to wait for send document reply. – ketan Apr 08 '21 at 13:38
  • @ketan Do you get the same behavior if you just put await before. `await Task.Run(() =>SendDocument());`? I think your method will need to be async too. – Dumisani Apr 08 '21 at 13:48

0 Answers0