Can somebody please compare these two logging mechanism?
This has been written about quite a bit. Here are some things to read:
http://james.newtonking.com/archive/2007/06/05/enterprise-library-logging-vs-log4net.aspx
http://theiterator.com/2009/01/log4net-vs-enterprise-library-logging-application-block/
https://stackoverflow.com/questions/118047/log4net-vs-enterprise-library-which-is-better-faster
My thoughts:
The general consensus seems to be that log4net performs faster. Whether this is meaningful in a typical application is up for debate.
log4net also supports hierarchical loggers out of the box which is nice. EL can perform something similar but you have to roll your own.
log4net's configuration is a bit arcane and not well documented (IMO). However, EL's is also cumbersome to configure (and painful without the config tool). Also EL gives you so much choice that you may want to spend some time on design (e.g. do you want categories to be related to the logging level or layers, or functional area, or all of the previous, or something else?)
If you are already using EL, you may want to just stick with EL Logging (it also integrates with the Exception Handling Block) for consistency. If I'm using EL for an application, then I tend to use EL for logging. If not then I usually favor log4net for smaller applications since the setup time is usually shorter. (Not to impugn anyone's favorite logger! :) )
I've used both and found that they both work well.
I've given some descriptions below all directly from the tool's website. I prefer ELMAH which is at the bottom, but have not spent much time on Log4Net or the Logging Application Block.
Log4Net
Support for multiple frameworks
Output to multiple logging targets
Hierarchical logging architecture
XML Configuration
Dynamic Configuration
Logging Context
Proven architecture
Modular and extensible design
High performance with flexibility
Logging Application Block
The event log
An e-mail message
A database
A message queue
A text file
A WMI event
Custom locations using application block extension points
ELMAH
Logging of nearly all unhandled exceptions.
A web page to remotely view the entire log of recoded exceptions.
A web page to remotely view the full details of any one logged exception.
In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
An e-mail notification of each error at the time it occurs.
An RSS feed of the last 15 errors from the log.
This might be of interest: http://www.dotnetlogging.com/comparison/
Related
I'm really new to logging, and I've decided it's time to take the plunge and start working it into my applications from now on.
I'm going to be using NLog as I hear it's very fast and simple to set up.
What type of information should I be concerned about logging? What information should be considered superfluous and unnecessary?
On a more specific line, where should I be placing my _logger.Info("Foo"); calls? Within each ActionMethod in my controllers?
You should look at ELMAH
It is the easiest thing to setup using nuget. And it provides awesome logging for asp.net MVC (logs all exceptions and the request associated with it), all the effort you have to do is to install the package in your application. :)
http://code.google.com/p/elmah/
The importance of logging cannot be explained in a word or two.
Logging forms an essential part of investigation by your application support L2 and L3 teams. They need the logs to respond to what has happened, bugs and so on.
Since logging forms non functional concern of any application, it is a cross cutting concern in terms of Aspect Oriented Programming, many aop frameworks like aspectJ is used in the Java development wing to implement logging, together with mature loggers.
http://www.dotnetlogging.com/ provides good tools for logging in .net
We use log4net and ELMAH. ELMAH is used for anything top level or unhandled.
We log (log4net) every method. We wrote a macro to inject logging enter/exit code (although there is a product out there that will inject this instrumentation in your compiled code)
For other steps we log out debug info (loading record id 15) log.Debug(...)
other times info "processing customer JOHN' using log.Info(..)
when we catch an exception we
log.Error()
Then we deploy our application and set our logging level to debug for the first bit of time, then switching over to "info" once its running smoothly.
This provides quite a bit of detail, but its up to you to decide where. Im just saying we log details in virtually every method.
also remember when you catch exceptions and log them, do now rethrow them. IE DO NOT
throw ex;
just simply
throw;
log4net can be found here:
http://logging.apache.org/log4net/
and a nuget package is available for automatic configuration at
http://nuget.org/List/Packages/log4net
ELMAH is available via nuget as well for automatic configuration although there are extra steps you need to take to work with MVC. Check out:
How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?
I have a web application, and I would like to audit most of the users actions on the application, for example login, insertion to db, update to db, fired exceptions, etc.
One of my senios suggested using a queue for faster performance, so you just enqeue an event and it is then processed automatically without having to wait for it to be processed.
What are your suggestions? How should I process them? Enqueueing them is no problem, but I'm not sure how they will be processed then without no body calling a method.
I am using C# with .NET 4.0
I've been working on a library that can probably help.
Audit.NET and its extensions to audit different systems (WCF, MVC, WebApi, EF) and store logs in different data storages (SQL, MongoDB, DocumentDB, File, EventLog) will give you the flexibility to configure what do you want to audit and where do you want to store the audit logs.
I would simply recommend an off the shelf logging framework that is stable and supported. Have you considered a logging framework, like log4net?
You could write a custom appender for logging into MSMQ if you'd like.
An alternative logger is called TracerX. It is written in C# and fast and flexible. Because the source code is available it means you can modify it as you wish to suit your needs. It comes with a viewer that allows for filtering the output.
https://github.com/MarkLTX/TracerX and an article on how to use it:
http://www.codeproject.com/KB/dotnet/TracerX.aspx
Two topics of interest actually:
Asynchronous logging
Aspect Oriented Features
Asynchronous logging may speed-up heavy processing 100-fold. Use a writer thread that dumps the queue into log sink every,say 100ms however that Logging engine must be deterministically started and stopped so it can flush the sinks on application stop.
Aspect Oriented Programming addressed your cross-cutting concern - audit/log calls shall be invoked in desired operation prologues/epilogues - look at PostSharp project.
(Little late on the answer, this post shows up high in google, so I thought it may be worth looking at some of the options)
If you are looking to actually audit. (By this I mean to record that an action took place, who did it and when, and for that auditable log to be able to be used as evidence to an external auditor)
(Debug Logging vs Auditing logging)
If so, you can consider some options, such as:
use an Audit logging library
adopt an EventStore database
use a logging library that fails loudly
1. using an audit library
Audit.NET has already been mentioned here and has an impressive number of downloads and is very feature-rich
auditable - an alternative to the above (disclaimer, its written by me)
both are pretty cool, as they allow you to bring your own datastore
2. Eventsourcing
EventStore
Postgres with Marten
The design here (which can impact your architecture to embrace Events) is that Events are immutable, and if you store them then you have an auditable store of things that happened in your system
note this does not look to solve the question above, but it does solve how to audit, so I have mentioned it
3. Logging library
Serilog - Issue
you have to confirm that the logging library if it fails to add an Audit Log, it will throw an exception.
if it does not do that then you will be missing auditable logs, which then you cannot build trust with your Auditors
Side note 1 - with options 1 and 3, you may need to ensure that the log is written in the same transaction as your primary data store. to ensure that all of the information is ACID. (this is similar to the issue people have with publishing an event which is outside of the database transaction)
Side note 2 - that audit logs should be able to identify who did what, so you may/should need to encrypt the datastore they eventually end up in.
In my team there is discussion about web application exception logging, regarding whether we should log to a text file or the event log.
Can anyone please tell me which is the better way of exception logging occuring in the application? Either one of the two I've already mentioned or is there a better option?
Don't reinvent the wheel - use an already existing logging library/tool such as log4net or ELMAH instead of writing your own logging library. Both give you multiple choices for where you log exceptions to, it's up to you which you prefer.
Two things to consider:
1 - Where do you log to if you're logging to the database, but the database is unavailable / under heavy load?
2 - Windows Event Logging is very light-weight and event log forwarding means you can automagically aggregate logs from multiple machines without writing any code.
Another great and easy to use Logging-Library is NLOG
From the site:
"Supported targets include:
Files – single file or multiple, with
automatic file naming and archival
Event Log – local or remote
Database
– store your logs in databases
supported by .NET
Network – using
TCP, UDP, SOAP, MSMQ protocols
Command-line console – including
color coding of messages
E-mail – you
can receive emails whenever
application errors occur
ASP.NET trace
… and many more
Other key features:
very easy to configure, both through configuration file and programmatically
easy-to-use logger pattern known from log4xxx
advanced routing using buffering, asynchronous logging, load balancing, failover, and more
cross-platform support: .NET Framework, .NET Compact Framework and Mono (on Windows and Unix)"
I prefer a text file because it allows for more flexibility and easier navigation. I suppose it's a matter of preference, but the navigation between errors in the Windows event log seems very cumbersome. In addition to the cumbersome navigation it includes errors that you don't care about. If you can define the format and the content then it's much more efficient.
At the app I work, we are used to log every exception at the database, so this way is easier to link the exceptions details to the "user error report" they are requested to fill (which is at our the default error page). It helps us on statistics and as already said, linking exception details to the user description of the error (like, which steps he took to get that error, etc...)
Disclaimer: i am the developer of KissLog.net logging application.
log4Net and Elmah are very good, but they have limited capabilities.
Elmah doesn't support trace / log messages, and logs only the exceptions, and log4Net doesn't provide a user interface to navigate through the logs.
You can try to use KissLog.net, it is an application where i tried to aggregate all the necessary logging features (capture errors, log / trace messages, easy to use user-interface)
I'm wondering if there's any way of having some sort of Aspect-Oriented way of setting up logging of C# code. Or if the code could be instrumented for automatic logging.
At the moment the code is riddled with Log("Enter method XXX") and Log("Leaving method XXX") which make maintenance really tedious.
Ideally I'd like to have something that does the logging automatically the same way as the libraries are instrumented for profiling.
The next best thing would be to have some custom attributes maybe that I can tag my methods with. These would put some logging code on entrance and exit of the method.
And if the solution were compatible with the EntLib that would be perfect :)
Cheers.
If you're using the Enterprise Library, you have everything you need. Take a look at this article: http://www.codewrecks.com/blog/index.php/2009/01/31/unity-and-aop-in-enterprise-library/
You could use Log4PostSharp. I am not sure though what the future of this looks like as PostSharp went commercial.
What your referring too is a cross cutting concern, and not only affects your application but other applications that you might install at your establishment. The Enterprise Blocks are great and the inversion of control principal does help a lot with extracting repeating code from out of the system. However there is no way of logging without deciding some place in your code that you wish to record the event. for example exceptions, logging in, logging out, db actions, restricted actions etc. If you go the Enterrpise route its all done through configuration files and policies.
In the solutions I have provided, I have moved the logging functionality outside of the application space and it now sits aside every piece of code that I develop, ready and waiting to do the logging for me. On the last project I used a combination of Enterprise Blocks and Couchdb. Couchdb really helps with the aspect side as it works using REST and Json without involving itself too much in your application writing an interface to the log files is just a matter of a bit of HTML, it really is a fire and forget type affair, until that bad ol day when you need to scour the logs :)
The only problem that I have seen in applications where you automate the logging is that you use some sort of delegate process and pass things into them, which increases stack space. But this is so trivial that its beyond reason.
Program to interfaces and defined interfaces and you should be okay.
I remember something regarding Interceptors / Proxying to log entry/exit of methods.
Stack Overflow question - How do I intercept a method call in C#?
and check out this blog (ref'd in the same question) - http://madcoderspeak.blogspot.com/2005/09/essential-interception-using-contexts.html
First of all, apologies for the subjective sounding title. This is intended as a direct question.
At present I am working on a suite of tools:
A C# Windows Service, to primarily
maintain an Oracle database.
A C# Windows Service, (which will be
used on multiple node sites) to
process content of the database.
An ASP.NET web interface to
facilitate management of the overall
"system"
Currently the tho Windows Services have been developed as Console Applications (to ease debugging/development) and I am in the midst of converting these to Services. After testing for a couple days now with these services, I'm finding that I would like to increase the granularity of my logging. I'm finding that I miss Console.WriteLine() and I would like to provide an alternate log source like a flat-file for this type of output. This has lead me to think, "Should I be using a framework, or have I got enough?"
The reason I have mentioned the aspects I am developing is to provide insight to my situation. A "Core" DLL has been created, common across all components, abstracting the interaction layer between the applications and database. It is within this DLL that a class has been created which will attempt to "log to a table in the database" else on fail "log to local Event Log". This is it, that's the extent of logging.
Throughout the aforementioned tools, there are multiple instances of logging not dissimilar to:
Log.LogError("Code", e.Message + "\n" + e.StackTrace);
Although quite basic, this method does make use of reflection to Identify the source of the error.
My Question
Looking at my current logging solution it appears "sufficient" in terms of what it does and how it is integrated with all my solutions. However, I've been looking at logging frameworks (Notably log4net) and their features impress me. The ability to, if needed in the future, add another output format (such as an SMTP server) sounds kind of cool to me! :)
What I would like to know are the benefits of moving to a framework (like log4net)? The extent of how much I will have to adapt my code? Whether or not I am just looking at the greener grass on the other side? And finally, but probably most importantly, am I doing the right thing? Should I just add the ability to my Log class to "LogDebug" and be done with it? The last thing I would want to do is completely overhaul my suite, just for a "basic" feature, but if there are other benefits (to design, reliance, good practice? etc.) I'm interested.
Thanks,
Yes. Using an existing, proven logging framework (such as Log4net) is a good idea.
Log4Net is configurable at runtime (great for tracking down issues in production code).
As a commenter pointed out, it's also very simple to use.
Proper logging is especially beneficial when running code on multiple remote systems, as far as I recall, log4net will let you send your logs to a remote syslog server without much coding overhead (meaning you can view your logs from all machines in one centralized place) doing this will massively reduce the time it takes you to get information relating to a bug or problem with the system, and should also give you an indication of how prevalent the issue is.
As mentioned in other posts, log4net also allows for multiple appenders and multiple log levels, so determining where you want certain log information (i.e. in a database or in a local flat file, hey log4net even lets you spit logs out over telnet) to be stored is an absolute doddle.
As for implementing it, there are several good sites talking you through the setup. How you actually make use of the logging objects that log4net gives you is an architectural choice, but you could simply change the constructor of an object to take a log4net object and from within this object, just use the log4net object as you would Console.WriteLine.
I find the tutorial series here particularly useful, and it'll also go in to more depth than I can here about the benefits and the different ways of configuring log4net.
Yes, you definitely want to use a logging framework. A logging framework will allow you to:
Set the logging levels for the different logger instances.
Set the "appenders" or output for each of the different logger instances.
Perhaps, more importantly, if you use a logging framework, it is very easy to swap out one implementation of the logging framework for another (perhaps a null implementation that simply discards messages); whereas, if you write all your logging statements, directly, swapping out the implementation will be a nightmare.
I think you should use Log4net, simply because it's always better to reuse than to build your own thing. log4net has been used by a lot of developers and are pretty matured.
Think about your maintenance prospect; one or two months down the road, you might need to tweak your custom logging class a bit, to add some multithreading support etc. And when you are fixing the bugs arose from your logging class, you will miss Log4net.
Well one of the bigger benefits is not having to maintain the code yourself. Most of the time, logging frameworks have a lot more functionality than your own solution. Because they are so focused on logging, those frameworks usually are pretty complete in the both functionality and ways to implement it. And then there's reliability; there's nothing worse than a logging framework that's not logging anything because it's bugged. ;)
Take for example ELMAH for ASP.net applications. It also includes notifications, exports to various target formats, etc. Things that are pretty handy but you'll never build yourself unless you really need it.
How many changes to your code are needed obviously depends on both your code and the framework of choice. It's hard to say anything about that.
I am going to give a shout out to NLog (http://nlog-project.org/home) as it doesn't suffer from the 'Straight Java Port - then rewrite' syndrome of most oss .Net libs.
Some key benefits for us were the very fast Logger.IsFooEnabled (volatile read) and the overall performance of the system.
To each its own though, but I personally prefer NLog for my projects (and some of my clients too).
Cheers,
Florian
The advantage of using a good logging framework like Log4Net is that they have a small impact upon your code in terms of lines of code that you have to alter (in other words you only have to alter each existing logging line).
Also, if you are concerned about altering your code if you change frameworks, or if you feel you want to roll your own, then you could always create your own interface to a logging framework. Then you only ever have to change your code in one place after that.
I think sysadmins expect services to log to the application event log in windows.
Look up System.Diagnostics.EventLog, although log4net will write to that too..
The initial statement in the log4j website might help in some of your questions, the underlying principles are the same of log4net:
With log4j it is possible to enable
logging at runtime without modifying
the application binary. The log4j
package is designed so that these
statements can remain in shipped code
without incurring a heavy performance
cost. Logging behavior can be
controlled by editing a configuration
file, without touching the application
binary.
Using a logger hierarchy it is
possible to control which log
statements are output at arbitrarily
fine granularity but also great ease.
This helps reduce the volume of logged
output and minimize the cost of
logging.
In this case there's clearly no need to reinvent the wheel. Most Logging frameworks are somewhat straightforward, so the extend of changes will most likely depend on the size of your existing programs.
if you write your logger class properly it will be easily expendable to any of your needs. Any framework could impress you with many features but another framework is another variable in your debugging process as it can give you an error that does not exists or can make an error by itself in combination with your application. If you are ready to make beta testing for open source software project this is fine...
In your place i would write log class with ability to extend it features you find interesting to you project based on the list of features known frameworks have. I don't see any problem to log something to file and then send it over smpt, just one small function does the job.
Moreover, you can write your own class which will be pretty abstract and put your basic code in there, if you will ever need to use external framework for testing you class would be able to use it with minimal impact on code. Just take a look how there frameworks are implemented on the code level.
think of that you will need to learn how to properly use these frameworks when your only needs for now to log very small part of it...