Skip to content

Log4Net appender for streaming log events directly to web client using SignalR

Notifications You must be signed in to change notification settings

ChrisFulstow/log4net.SignalR

Repository files navigation

#log4net.SignalR

Log4Net Appender for sending log events from server to browser

log4net.SignalR is a Log4Net Appender that sends Log4Net events logged on the server to a JavaScript function in the browser. It uses the SignalR async signaling library to stream these events in real-time over a persistent connection between the server and client.

The main use case for log4net.SignalR is building a log viewer on your site that gives easy visibility to diagnostic information and errors logged on the server. (However, you probably won't want to use it in production in case your logs include sensitive information.)

###Feedback

Questions or feedback? Tweet me: @ChrisFulstow

##Getting started

Getting started is easy. You can also check out the bundled MvcExample project to see some examples.

###Add log4net.SignalR.dll

Add the compiled log4net.SignalR.dll assembly or the source files to your project.

###Configure log4net.SignalR as a Log4Net appender to a self-hosted hub (for example, messages logged from within a web application)

Configure the SignalrAppender as an output destination for your log events by adding this to your log4net configuration (usually web.config):

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net debug="true">
    <appender name="SignalrAppender" type="log4net.SignalR.SignalrAppender, log4net.SignalR">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %-5level - %message%newline" />
        </layout>
    </appender>
    <root>
        <appender-ref ref="SignalrAppender" />
    </root>
</log4net>

###Configure log4net.SignalR as a Log4Net appender to a remotely hosted hub (for example, messages logged from a console application, but displayed in a web application)

Configure the SignalrAppender as an output destination for your log events by adding this to your log4net configuration (usually log4net.config or app.config):

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net debug="true">
    <appender name="SignalrAppender" type="log4net.SignalR.SignalrAppender, log4net.SignalR">
		<proxyUrl>http://localhost/</proxyUrl> <!-- Note: This should point to the root of your Web Application, not the hub itself -->
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %-5level - %message%newline" />
        </layout>
    </appender>
    <root>
        <appender-ref ref="SignalrAppender" />
    </root>
</log4net>

###Set up a page to listen for events

Add some jQuery to your ASP.NET page to listen out for events raised on the server. Once the SignalrAppender is set up, all events logged on the server using Log4Net will be transmitted to the browser by executing a JavaScript function.

Here we're adding each event's details to an HTML table, but you can use the onLoggedEvent callback and the log details passed in the loggedEvent parameter object to do anything you like.

    $(function () {
        var log4net = $.connection.signalrAppenderHub;

        log4net.client.onLoggedEvent = function (loggedEvent) {
            var dateCell = $("<td>").css("white-space", "nowrap").text(loggedEvent.TimeStamp);
            var levelCell = $("<td>").text(loggedEvent.Level);
            var detailsCell = $("<td>").text(loggedEvent.Message);
            var row = $("<tr>").append(dateCell, levelCell, detailsCell);
            $('#log-table tbody').append(row);
        };

        $.connection.hub.logging = true; // turn signalr console logging on/off

        $.connection.hub.start(function() {
            log4net.server.listen();
        });
    });

###To test the MVC example Open up two browser windows/tabs. Keep one tab on the initial page. Use the other tab instance to navigate. You will see the log4net messages accumulate in the first tab.

A note about running the SignalrAppenderHub within an ASP.Net web application

If you are hosting the hub from within an ASP.Net application (which you most likely are), you should also make sure that you also tell IIS to map the proper URLs for the hubs. You should add the following statement somewhere in your Global.asax.cs.

app.MapSignalR(new HubConfiguration {
                EnableDetailedErrors = true, //Do this to help debugging, set to false in production
                EnableJSONP = true, //Do this if any of your SignalR hubs will be called by a proxy hub (like an appender in an external process)
                EnableJavaScriptProxies = true
            });

##License log4net.SignalR is open source under the The MIT License (MIT)

About

Log4Net appender for streaming log events directly to web client using SignalR

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •