This project is read-only.

Message, Operation, and Exception Logging Examples/Guidance?

Topics: Web Api
Jul 29, 2011 at 6:18 PM
Edited Jul 29, 2011 at 6:18 PM

How are people handling message and operation logging at a framework-level?

I've got a "MessageHandler" (DelegatingChannel) that looks like:

    Public Class LoggingMessageHandler
Inherits DelegatingChannel
        Private ReadOnly mLogger As ILogger

        Public Sub New(innerChannel As HttpMessageChannel)

            ' TODO: Move to DI instead of ServiceLocator
            Me.mLogger = ObjectFactory.GetInstance(Of ILogger)()
        End Sub

        Protected Overrides Function SendAsync(request As HttpRequestMessage, cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)
            ' TODO: It would be nice to get the calling client IP logged.
            Dim logEntry = String.Format("{0} {1} {2}",
                                         request.Headers.From, request.Method, request.RequestUri)


            Return MyBase.SendAsync(request, cancellationToken)
        End Function
    End Class


I'm not able to get all of the information I would like to log at the message level here.  Client IP being an example.  The lack of built-in DI support for this class too makes me think I might be doing message logging at the wrong spot?  Plus I didn't know if I should be overriding Send or SendAsync or both?

I toyed around with OperationHandlers to do cross-cutting, operation-level logging, but wasn't having much luck there either.

Finally, exception logging at the framework level.  I've totally punted on this one so far.

Anyone have any examples to share of logging at these three levels?  I've poked around at the Contrib project, and found a workaround for the DI issue on MessageHandlers and looked at their logging examples, but nothing really jumped out at me as being the right way to do it.