Click here to Skip to main content
15,867,835 members
Articles / Operating Systems / Windows
Article

LOG4NET WebServiceAppender

Rate me:
Please Sign up or sign in to vote.
4.25/5 (9 votes)
27 Dec 2006CPOL3 min read 55.4K   1.1K   32   6
Introducing a small appender that logs to a web service within the LOG4NET logging framework


Please note that this is the worst UI I've ever designed, but it's just enough for testing the functionality

Introduction

The LOG4NET (or "log4net"? I don't know what casing is correct) logging framework is a set of classes for logging text messages from within a .NET application to various target locations. Usually this is done for debugging or tracing purposes.

These target locations can be locations like e.g. log files, SMTP or databases and are filled through what LOG4NET calls "Appenders". The appender architecture is configurable through configuration files and is rather extensible.

This article describes an appender that logs to a web service. The appender will be called WebServiceAppender.

The download includes the actual appender class as well as the web service backend to receive the logging requests and dispatch them further.

Background

The project introduced here makes use of my own LOG4NET wrapper which is included in my set of base libraries "ZetaLibNet".

So if you want to just use the WebServiceAppender in you own project, simply copy the sample files to your solution and include them in your application structure.

If you want to get the maximum flexibiliy and minimum code size, I recommend that you get rid of my ZetaLibNet bindings. This is definitely possible and probably rather easy. I did not do it, simply because I'm lazy and because I do not have any requirements to do so.

Structure

The download consists of a Visual Studio .NET 2005 solution with three projects: "Runtime", "TestClient" and "Web".

  • "Runtime" - This is the actual library that contains the WebServiceAppender that logs to the web service. Since my example binds declaratively to the WebServiceAppender (through the "app.config" file of the "TestClient" project), I included a class "ForceLoad" which must be instantiated inside the "TestClient" project to force the linker to still bind to the "Runtime" project, even there is no compile-time binding to the library.
     
  • "TestClient" - A Windows Forms application that logs messages with the WebServiceAppender .
     
  • "Web" - A web project that contains the actual web service that receives the logging messages. It simply forwards the logged messages to the LOG4NET framework that is running on the server. Here, only the minimum functionality is implemented to give you an idea. I think there are much more advanced things you can do with the received messages. Be creative and please tell me!

Configuring the WebServiceAppender

Like every LOG4NET appender, you can configure this appender programmatically or declaratively. I've chosen the latter in my example.

Here is an example appender configuration with all available properties:

<appender name="WebServiceAppender" type="Zeta.WebServiceAppender">
    <!-- LOG4NET parameters. -->
    <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG"/>
        <param name="LevelMax" value="FATAL"/>
    </filter>

    <!-- General parameters. -->
    <param name="ApiKey" value="{0E12B21A-95B9-11DB-96C0-005056C00008}" />

    <!-- WebService parameters. -->
    <param name="Url" value="http://localhost:2259/Web/LoggingService.asmx" />
    <param name="TimeoutSeconds" value="60" />

    <!-- Proxy parameters. -->
    <param name="UseProxy" value="false" />
    <param name="ProxyUrl" value="http://myproxy:3128" />
    <param name="ProxyBypassOnLocal" value="true" />
    <param name="ProxyUseDefaultCredentials" value="true" />
    <param name="ProxyCredentialsDomain" value="OFFICE" />
    <param name="ProxyCredentialsUserName" value="MyUser" />
    <param name="ProxyCredentialsPassword" value="MyPassword" />
</appender>

See the "app.config" of the "TestClient" in the example downloadable project for details.

Behind the Scenes

The code, as always, is not much rocket science, but mainly some hard and/or boring work. I based much of my work on the skeleton provided by this guy (on the interesting L4N Dashboard website) and the examples in the LOG4NET SDK.

The web service call is done asynchronously in order to not block the caller when logging.

I provide a basic mechanism for ensuring that only allowed clients to log to the web service by accepting an API key. Usually an API key is a GUID (string) that you as the web service hoster supply to the clients that are allowed to log. The web service then checks whether you are allowed to log by checking the API key that must be sent with every logging request.

Conclusion

This short article shows you a short and hopefully practical way of enhancing the great LOG4NET logging framework to log to a web service. I wrote the classes and the article itself solely because I found no ready-to-use solutions on the web (I only found one similar article but for LOG4J only) and I think it is a great idea for applications to log to a remote location.. Hopefully I did a small step to fill this gap.

Please use and enhance the code just as you like. I'm more than glad if you write comments, suggestions, bugs and enhancements in the comment section below. Thank you!

History

  • 2006-12-27: First version created and uploaded.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
QuestionHow can it work with a old version of log4net(1.2.9.0) ? [modified] Pin
xmuczg25-Jun-07 14:55
xmuczg25-Jun-07 14:55 
GeneralSeparating WebService Appender Pin
heelslide2-Feb-07 13:09
heelslide2-Feb-07 13:09 
GeneralRe: Separating WebService Appender Pin
Uwe Keim3-Feb-07 0:09
sitebuilderUwe Keim3-Feb-07 0:09 
GeneralRe: Separating WebService Appender Pin
oldefezziwig20-Jul-09 7:28
oldefezziwig20-Jul-09 7:28 
GeneralRe: Separating WebService Appender Pin
Uwe Keim20-Jul-09 7:34
sitebuilderUwe Keim20-Jul-09 7:34 
GeneralThanks Pin
George_Saveliev31-Dec-06 10:50
professionalGeorge_Saveliev31-Dec-06 10:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.