title: Why HttpModule of my ASP.NET Web API app is not being hit on remote server?
body: <p>All I need is to log HTTP requests/responds to db and log some other stuff to file.</p>
  
  <p>I try to <strong>log message into file</strong> and <strong>insert data into db</strong> during the execution of <em>OnApplicationEndRequest(object sender, EventArgs e)</em> method of my custom HTTP module (<em>BasicAuthHTTPModule.cs</em>) in my ASP.NET Web API application. My custom HTTP module follows this structure (with modification in <em>OnApplicationEndRequest(object sender, EventArgs e)</em> ) => <a href="https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/basic-authentication#basic-authentication-with-custom-membership" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/basic-authentication#basic-authentication-with-custom-membership</a>.</p>
  
  <p>I have investigated 2 cases where the application behave differently and I don't know why...</p>
  
  <p><strong>case 1)</strong>
  When I run this project from Visual Studio, database insertion is performed fine, but file insertion isn't performed at all = nothing happens... Btw I am able to log into file from Controller, but no from HTTP Module. So it is not possible to log into file from HTTP Module class?</p>
  
  <p><strong>case 2)</strong>
  When I host my ASP.NET WEB API on remote server and I consume the API from my local PC's console application, database insertion is not performed, neither file insertion. So nothing at all is performed in this case. It looks like that my custom HTTP module is not being hit in this case...</p>
  
  <p><strong>Here are my code snippets:</strong></p>
  
  <p>BasicAuthHttpModule.OnApplicationEndRequest(object sender, EventArgs e)
  tasks:</p>
  
  <ul>
  <li>insert log into db</li>
  <li>insert log into file</li>
  </ul>
  
  <pre><code>switch (response.StatusCode)
  {
      case 200:
          if (endPoint == "/api/cards")
          {
              DatabaseEngine.sInsertIntoWebApiLogTable(); // inserts into db + saves changes
              WebApiConfig.myLogger.Information("success");
          }
          break;
          .
          .
          .
  }
  </code></pre>
  
  <p>DatabaseEngine.sInsertIntoWebAPILogTable()</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Adds new record to C___WEBAPI_LOG and Saves Changes
  /// &lt;/summary&gt;
  public static void sInsertIntoWebAPILogTable()
  {
      C___WEBAPI_LOG newWebAPILog = new C___WEBAPI_LOG()
      {
          CLIENT_IP               = "test1",
          REQUEST_ENDPOINT        = "test2",
          REQUEST_HTTP_HEADER     = "test3",
          REQUEST_TIMESTAMP       = "test4",
          RESPONSE_HTTP_HEADER    = "test5",
          RESPONSE_BODY           = "test6",
          RESPONSE_TIMESTAMP      = "test7",
          RESPONSE_STATUS_CODE    = "test8"
      };
  
      m_cnDbEngine.C___WEBAPI_LOG.Add(newWebAPILog);
      m_cnDbEngine.SaveChanges();
  }
  </code></pre>
  
  <p>WebApiConfig.cs</p>
  
  <pre><code>public static class WebApiConfig
  {
     public static Serilog.Core.Logger myLogger;
  
     public static void Register(HttpConfiguration config)
     {
         // Web API configuration and services
  
         // Web API routes
         config.MapHttpAttributeRoutes();
  
         config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );
  
         config.MessageHandlers.Add(new ThrottlingHandler()
         {
             Policy = ThrottlePolicy.FromStore(new PolicyConfigurationProvider()),
             Repository = new CacheRepository(),
             //Logger = new Modules.TracingThrottleLogger(traceWriter)
          });
  
          myLogger = new LoggerConfiguration()
                      .MinimumLevel.Debug()
                      .WriteTo.RollingFile("logs\\HelloWebApi_auth-{Date}.txt")
                      .CreateLogger();
      }
  }
  </code></pre>