https://github.com/angularsen/AspNetHttpLogger
Log raw HTTP requests and responses in ASP.NET WebApi and WebApi2.
https://github.com/angularsen/AspNetHttpLogger
Last synced: about 2 months ago
JSON representation
Log raw HTTP requests and responses in ASP.NET WebApi and WebApi2.
- Host: GitHub
- URL: https://github.com/angularsen/AspNetHttpLogger
- Owner: angularsen
- License: mit
- Created: 2014-09-21T09:48:45.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-04-20T18:46:50.000Z (about 7 years ago)
- Last Synced: 2025-05-01T17:31:40.268Z (about 2 months ago)
- Language: C#
- Homepage: https://www.nuget.org/packages/AspNetHttpLogger/
- Size: 689 KB
- Stars: 3
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
AspNetHttpLogger
================
Log raw HTTP requests and responses with ASP.NET WebApi and WebApi2.### WARNING! This approach is not working with OWIN pipeline
I have not addressed this code in ages and it broke when OWIN was introduced in ASP.net.Please see this issue for a possible workaround I have not verified:
https://github.com/angularsen/AspNetHttpLogger/issues/3If you are using IIS you can also consider using Failed Request Tracing as detailed in https://github.com/angularsen/AspNetHttpLogger/issues/5.
### Step 1 - Install nuget
To install AspNetHttpLogger, run the following command in the [Package Manager Console](http://docs.nuget.org/docs/start-here/using-the-package-manager-console)
### Step 2 - Register LoggingHandler as a message handler
```csharp
var loggingHandler = new LoggingHandler();
loggingHandler.ResponseCompleted += DoSomething;GlobalConfiguration.Configuration.MessageHandlers.Add(loggingHandler);
```### Example code
**Global.asax.cs**
```csharp
public class MvcApplication : System.Web.HttpApplication
{
public void Application_Start()
{
// Other setup stuff to peform on startup..
GlobalConfiguration.Configure(EnableRequestResponseLogging);
}private void EnableRequestResponseLogging(HttpConfiguration httpConfiguration)
{
var loggingHandler = new LoggingHandler();
// Hook up events
loggingHandler.InternalError += LoggingHandlerOnInternalError;
loggingHandler.ResponseCompleted += LoggingHandlerOnResponseCompleted;// Register as a message handler to peek at all requests and responses
httpConfiguration.MessageHandlers.Add(loggingHandler);
}///
/// A request/response just completed. See for more details.
///
/// Details about the request and response. Call for a pre-formatted string output.
private void LoggingHandlerOnResponseCompleted(LogEvent logEvent)
{
Log.Debug(logEvent.Summary + "\n" + logEvent);
// Log the raw request/response output to our Loggr logging service
var loggr = DependencyResolver.Current.GetService();
loggr.PostEvent(logEvent.Summary, logEvent.UserName, logEvent.Request, new[] {"api", "log", "raw"}, logEvent.ToString(),
LoggrDataType.PlainText);
}///
/// An exception occurred in the . It will be silently ignored,
/// but you can use this event to log and track down issues with the handler.
///
/// The .
/// Exception that occurred.
private void LoggingHandlerOnInternalError(HttpResponseMessage response, Exception ex)
{
Log.ErrorException("Exception occurred in LoggingHandler.", ex);// Send information about the error to our Raygun exception tracking service
var raygun = DependencyResolver.Current.GetService();
raygun.SendAsync(ex, new[] {"LoggingHandler"});// Log the exception
var loggr = DependencyResolver.Current.GetService();
loggr.PostErrorEvent(ex, "LoggingHandler: " + ex.Message, User.Identity.Name, response.RequestMessage,
new[] {"api", "exception", "handled"});
}
}
```### Feedback
This is so far the best way I have found to accomplish this in ASP.NET WebApi and WebApi2. If you know of a better way or have any improvements, please sound off in the Issues page.