Good Way to develop a Logger DLL for web app and windows app

534 Views Asked by At

Let's start by the context !

At my job, I have been asked to add some log functionality to my project, a web app in ASP.NET.

What I thought is that well, I could have do it just for my project, but that's not very useful! So I want to create a DLL that can be used in others projects (webapps or windows apps).

So I planned these functionalities:

  • XML File for configuration : LogLevel of the file, location of the file, time for keeping it, max size before writing in a new log file. For example I can have a log file where will be written only ERROR and CRITICAL logs and another file where will be written INFO and WARNING LOGS.

    • Ability to log for a web app or for a win app

But here is the problems, I'm thinking of differents things that can go wrong and I wanted to be sure of what I do before developping it.

First of all, I will have to detect if it's a web application or a windows application. I have find some things that can be helpful like this one

Then I'm thinking about how logs will be written, each time I will write a line of log, I will have to open and close my file. I feel like it's something very greedy in performances. Am I wrong ? Is there any other way to do it ?

It should also be pointed out that there could be a problem when logging from both web applications or a multi-threaded windows applications. Another issue is that the log method could be called twice (2 times) at once, which will then create a problem of simultaneous access for the log file. What is the best way to deal with these issues?

At last, I wonder if you see any other things that can be a problem for this kind of dll ?

Thanks for your help ! :)

2

There are 2 best solutions below

0
On

You could give a try to ELMAH https://elmah.github.io/ or to Serilog : https://serilog.net/ Both, as log4net (maybe less than log4net), are "classics". As previous answer says, there is no gain, except maybe a kind of coding fun, to reinvent the wheel.

3
On

Its not worth it to reinvent the wheel. You can have a look at log4net library. Log4net supports writing the logs into TXT, XML, SQL, MSMQ etc. What I would suggest is, writing a bunch of logs is going to block your application execution to certain extent. So instead of doing TXT or SQL logging, I would go for MSMQ logging (Its fire and forget which doesn't blocks your application code). Have a windows service to read the message queue and write the logs in a separate SQL database.