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.
You cannot define the object
Conexao con
to a variableprivate
and if it is already set, you will only reuse the same "connection" if there is more than one LOG in the same request?– Guilherme Nascimento
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.
– Andrew Alex