Reputation: 39
All I need is to log HTTP requests/responds to db and log some other stuff to file.
I try to log message into file and insert data into db during the execution of OnApplicationEndRequest(object sender, EventArgs e) method of my custom HTTP module (BasicAuthHTTPModule.cs) in my ASP.NET Web API application. My custom HTTP module follows this structure (with modification in OnApplicationEndRequest(object sender, EventArgs e) ) => https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/basic-authentication#basic-authentication-with-custom-membership.
I have investigated 2 cases where the application behave differently and I don't know why...
case 1) 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?
case 2) 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...
Here are my code snippets:
BasicAuthHttpModule.OnApplicationEndRequest(object sender, EventArgs e) tasks:
switch (response.StatusCode)
{
case 200:
if (endPoint == "/api/cards")
{
DatabaseEngine.sInsertIntoWebApiLogTable(); // inserts into db + saves changes
WebApiConfig.myLogger.Information("success");
}
break;
.
.
.
}
DatabaseEngine.sInsertIntoWebAPILogTable()
/// <summary>
/// Adds new record to C___WEBAPI_LOG and Saves Changes
/// </summary>
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();
}
WebApiConfig.cs
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();
}
}
Upvotes: 0
Views: 537
Reputation: 363
I know this might be too late already, but depends on the IIS version your running on your local machine and remote server.
For IIS 7 and above Your modules should be set in <system.webServer>
But for older version, it should be set in <system.web>
See here for more info on that: https://mvolo.com/breaking-changes-for-aspnet-20-applications-running-in-integrated-mode-on-iis-70/
Upvotes: 0