Ahmed
Ahmed

Reputation: 525

How to log an API response using log4net in WebAPI application

I'm new to log4net & I need to log an API response. This is how I configured it in web.config

<log4net>
    <appender name="StandardAppenderSync" type="log4net.Appender.RollingFileAppender">
      <!-- The standard pattern layout to use -->
      <file value="log\Log_" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <maxSizeRollBackups value="-1" />
      <maximumFileSize value="5GB" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <staticLogFileName value="false" />
      <datePattern value="yyyyMMdd'.txt'" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="StandardAppenderSync" />
    </root>
  </log4net>

And this is the code I'm using to log the response.

var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddHeader("header1", header1);
request.AddHeader("header2", header2);
request.AddParameter("parameter1", parameter1);
request.AddParameter("parameter2", parameter2);
IRestResponse response = await client.ExecuteAsync(request);

ILog log = LogManager.GetLogger("mylog");
log.Info(response.Content);

If I changed response.Content to "Test message" it logs normally. I believe the issue is that by the time this line is executed log.Info(response.Content); we aren't sure if the API has responded or no since it's async request.

How can I log the response as soon as the API responds without compromising the performance?

Upvotes: 0

Views: 1450

Answers (2)

Ahmed
Ahmed

Reputation: 525

Ok, I had 2 issues that were preventing it from logging correctly.

First thing was because of this line <file value="log\Log_" />. I copied this configuration from some website & I was looking for the logs in a different directory but it was generated in that one. A silly mistake. Once I corrected the path, it was capturing the API response as expected in my local machine. When I tried with "Test message", the path was correct, then I changed it to response.Content & changed the configuration at the same time, that's why it wasn't working in the directory that I was checking. One should do 1 change at a time & test it.

Then I published the changes to the staging environment & it wasn't working. I thought it's a permission issue, but after several attempts to fix it, I found that there was a missing dll file in the staging environment. The missing file called RestSharp.dll. This might help someone in the future.

Upvotes: 0

Rhys Jones
Rhys Jones

Reputation: 5508

If logging "Test message" works then the logging setup should be OK. By the time execution reaches the logging the API has responded (the await on the async method ensures this) so you might want to check the status code of your response or the length of the content.

If logging works in DEV but not in TEST/PROD then it could be a permission issue - the process doing the logging needs permission to write to the file location. Where possible use an absolute path to the log file (i.e. E:\Logs\MyApp\MyApp.log).

It's worth having a log message that logs at app start, with no conditional logic around it, so you always know for sure that logging is working.

Upvotes: 1

Related Questions