1

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 ! :)

Community
  • 1
  • 1
Grégory L
  • 612
  • 6
  • 17
  • 7
    While you certainly can write a nice, reusable log library, your time is probably better spent using an existing logging library. – Marc Talbot Jan 04 '17 at 14:08
  • 1
    Give a look at `log4net` or `nlog` or something else. They have already solved most of your problems. Why do you need to distinguish Web and Desktop apps? – Yuriy Tseretyan Jan 04 '17 at 14:19
  • You may be looking for this https://msdn.microsoft.com/en-us/library/ff648951.aspx – Pablo Recalde Jan 04 '17 at 14:21
  • For web apps using no to minimal code changes try Application Insights: https://azure.microsoft.com/en-us/services/application-insights/ – Peter Bons Jan 04 '17 at 14:28
  • Actually even if using an existing library can be a good and fast option. The answer for those question could be interesting, even if it's not for this project. – Grégory L Jan 04 '17 at 14:31
  • Answers can be found by looking at other implementations. You can take a look at the source code of many logging libraries on GitHub (for example https://github.com/NLog/NLog/). What you are asking is way too broad for a Stack Overflow question. – Peter Bons Jan 04 '17 at 14:56
  • Regarding the file open/file close challenge, you might find https://nblumhardt.com/2016/08/atomic-shared-log-file-writes/ interesting – Nicholas Blumhardt Jan 05 '17 at 22:51

2 Answers2

0

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.

  • I'm maybe trying to reinvent the wheel, yes. Maybe I will finally do it with an existing solution yes, and I'm pretty sure that all the solution given are very good. But anyway, rewriting that can be a good exercise for me. I'm still an apprentice at my job, I must learn. I knew there was some tools that could do it easily. But anyway, I want to try, even just to learn how to deal with those problems. Knowledges can't be useless I think... – Grégory L Jan 04 '17 at 18:13
  • It's perfectly reasonable to give it a shot - but using an industrial-strength logging library effectively is _also_ a skill worth learning, and one that will be quite valuable in the long run :-) – Nicholas Blumhardt Jan 05 '17 at 00:43
  • 1
    I totally agree and I took note of all your proposition and will probably give them a look soon. But I will anyway try to find those answer even just for me. – Grégory L Jan 05 '17 at 07:05
0

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.

Creep
  • 300
  • 3
  • 12