An open API service indexing awesome lists of open source software.

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.

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

If 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)
![nuget-installpackage](https://cloud.githubusercontent.com/assets/787816/4447419/adb615a8-480a-11e4-81a0-bd29c231ef4c.png)

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