Best practice to log system log

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 this way, that is, for every action I want to appear a log , it generates a insert in the database. This way, in addition to being costly, generates many requests to the database so I do not think it is the most viable, I was thinking of registering each log in a text file, then transmitting this file, would that be a good form?

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

My system has many features, so this log would be to map what is being used by the clients.

    
asked by anonymous 26.02.2015 / 19:26

2 answers

17

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

Of course some cases are interesting to create a systems own, but you need to have a reason for this. I do not think ready solutions solve all the problems. But it does not seem to be your case, I think you might as well use something ready that solves those issues that you are concerned about.

These tools provide buffering solutions, load balancing and troubleshooting, write asynchronously and allow you to choose multiple targets to persist log.

Before passing some solutions I mean that log in database has to be well thought out, imagine that the database can be a great source of problems in certain circumstances (unless which is a SQLite, for example), then you would need to log in the problems that occurred in your log . Complicated, right?.

Some framework of log known for .net:

  • Log4Net - one of the most well-known and used based on Log4J.
  • NLog - One of the most modern and well-known. The only one I actually used.
  • Enterprise Library - Microsoft's official solution but it does not solve some things well.
  • Elmah - Highly recommended for use with ASP.Net. Very simple to use because it injects what is needed.
  • dotNetLog - Another well recommended and has some different features.
  • SeriLog - I do not know but it's one more to investigate and see if it suits you better.

There are others that are simpler but which are unsupported and some are abandoned. There are also some more complete but paid solutions, rarely worth it.

In general, these solutions only take care of log itself, requiring you to invoke your non-interception run that probably requires assembly rewrite tools using aspects. See complementary answer by Cigano Morrison Mendez.

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

26.02.2015 / 19:44
11

Since what you want to register involves action trapping, the correct thing is to combine some @bigown response with dynamic proxy, this interception is done by Framework Castle Dynamic Proxy . However, because it is a Web application, you will have to register the triggers using a control inversion container, in this case the Castle Windsor .

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

If you want to only register persistencies and benchmark selections and your idea is to use the Entity Framework, use the EntityFramework.Extended , which has the audit log feature.

    
26.02.2015 / 19:59