Best practice for logging the system

Asked

Viewed 9,060 times

17

What is the best practice for registering a log system?

    public void inserirLog(string Acao)
        {

        StringBuilder csql = new StringBuilder();
        csql.Append("insert into Log (Acao,data) values(");
        csql.Append(@"""" + Acao + @""",");
        csql.Append("'" + DateTime.Now + "')");
        Conexao conn= new Conexao();
        conn.ExecutarComando(csql.ToString());
        }

Today I do it that way, I mean, to every action I want log, Gero a insert in the database. This way beyond costly generates many requests to the database so I believe not to be the most viable, I was thinking to register each log in a text file, then later transmit that file, would that be a good way?

Obs:. The logs are for action record , for example: "Opened Screen X", "Logou in System", "Exited System", etc.

My system has many features, so this log would be to map what has been used by customers.

  • You cannot define the object Conexao con to a variable private and if it is already set, you will only reuse the same "connection" if there is more than one LOG in the same request?

  • Thank you for the Reply, I will try to follow the advice of the friend below, but still thank you for the opinion. Well seen, do not need to instantiate the connection to each log could have used the same.

2 answers

19


The best practice is not to reinvent the wheel. It is to use a tool of log that has been well thought out and tested by professionals who dedicate themselves to this. And use these ready-made tools according to the specific recommendations.

Of course, some cases are interesting to create your own systems, but you need to have a reason for that. I don’t think ready-made solutions solve all problems. But it doesn’t seem to be your case, I think you might as well use something ready that solves those questions you’re worried about.

These tools offer solutions to realize buffering, do load balancing and fault resolution, write asynchronously and allows choosing various targets to persist the log.

Before I pass some solutions I want to say that log in database has to be well thought out even, imagine that the database can be a major source of problems in certain circumstances (unless it is an Sqlite, for example), then you would need log in the problems that occurred in its log. Complicates it, right?.

Some frameworks of log known to . NET:

  • Log4net - one of the best known and used based on Log4j.
  • Nlog - One of the most modern and known. The only one I actually used.
  • Enterprise Library - Official Microsoft solution but it doesn’t solve some things well.
  • Elmah - Highly recommended for use with ASP.NET. Very simple to use because it injects what is needed.
  • .NET Logging Framework - Another well recommended and has some different characteristics - I don’t know if it exists yet.
  • Serilog - I don’t know but one more to investigate and see if it suits you better.

There are others that are simpler but that are little supported and several abandoned. There are also some more complete but paid solutions, rarely worthwhile.

In general these solutions only take care of the log itself, requiring you to invoke your non-intervention execution which probably require rewriting tools from Assembly using techniques guidance to aspects. See complementary response from Gypsy Morrison Mendez.

There are still some tools to analyze these logs, which helps justify the choice for a standard solution.

  • I would add to this list the Tracesource... I don’t know why use a third party tool when dotnet itself provides a very complete solution.

  • Thank you I will take a look at these Framework. What I need is just to save each action log to know what customers are using on the system. For example: "Opened Screen X", "Logon to System", "Exited System", etc. Thank you for your response so far.

  • 1

    As the question possibly involves specific action interception, it would be good to improve the answer to include Castle Windsor and Castle Dynamic Proxy.

  • 1

    @Tobymosque the suggestion is good but to have a solution of log robust and more powerful would need more than this feature. I may be mistaken but it seems that this was not made to work as a log day-to-day.

  • @Ciganomorrisonmendez reply with this part. Although I have read something I do not know them, I would probably say nonsense.

  • @Bigown I’ll complement your.

  • @bigown, I especially don’t see much difference between the Tracesource and the Log4net, at least with my little experience with both. the only major advantage of Tracesource, is the initial configuration which is very simple.

  • I think he wanted to log for DB events...

  • The logs are for the same action record, or example: "Opened screen X", "Logou no Sistema", "Left the System", etc My system has many features, so this log would be to map what is being used by clients. Today saved every action in the BD, and then Gero a csv and transmit to the server.

  • @Rod was not what I understood in general log of DB events are generated by DB itself, no one needs to do anything but consult it and keep it.

  • I get it, I thought it was for auditing, but the actions, you could use google analyctics, know which urls are most accessed...if it was just to check what else he’s using...

  • @bigown yes...+1 for your reply, liked the Nlog, mainly by sending email when errors occur, this is very useful to my view =)

  • 1

    @Andrewalex in this case you will use the Filesystem as a target. Sending should be done independently. But study all options and test to see what suits you best.

  • @bigown Past links would be log frameworks ? ?

  • 1

    @Marconi yes, are, in general you call a method of it passing the information you want log in. But you can automate this. It has advantages and disadvantages in doing this. See Gypsy response to see one of the best known ways to intercept execution and "log in alone". The documentation of some of them also give tips on how to do this.

  • @Bigown Thank you very much I did not know or give existence of this, I will definitely be studying some.

Show 11 more comments

12

Since what you want to register involves interception of actions, the correct thing is to combine some log solution ( @Maniero response) with proxy dynamic, this interception being made by the Framework Castle Dynamic Proxy. However, because it is a web application, you will have to register the triggers using a container (container) control inversion, in this case, the Castle Windsor.

In Castle Windsor’s own tutorial, in part 5 of it teaches you to do exactly what you want.

If you only want to register database searches and selections and your idea is to use the Entity Framework, use the Entityframework.Extended., which has the audit log feature.

  • In my case the application is Winforms, and I will save this log file daily and then stream it to my ftp server ("If client is connected"). Then your suggestion would not enter into the correct context?

  • @Andrewalex And why wouldn’t I?

  • I will study this one a little then also, I understood wrong I thought that when you commented "for being a Web application" I was referring that I would need to use a Web technology.

  • No, no. I just diagnosed what you’d be using and took a few shortcuts ;)

  • 1

    @Andrewalex this solution is for you can automate the execution of log so you don’t even have to keep calling anything, you don’t touch your code. it log on your own. You may not need it, but it can be quite useful. Really understand its advantages before discarding, can save you a lot of work.

  • @All right, I won’t rule it out, now with these answers I got a lot of study material. And I saw that I need to understand better what the "LOG" is all about.

  • @Ciganomorrisonmendez Would you have any other examples to use Castle Dynamic? I could not understand how to "capture" automatically the execution of the log. Using log4net, I have already achieved a considerable advance, but now it remains to automate the process of log in.

  • @Andrewalex It would be by method interception, as described here. Note that the solution combines Castle Windsor with Castle Dynamic Proxy.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.