Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in a C# library

Tags:

What is the best practise regarding logging inside a library?

I am creating a C# library to be used by users and at some points I want to log an error or a warning. Is it a good practice to use log4net and log in a file?

like image 802
koumides Avatar asked May 04 '10 10:05

koumides


People also ask

What is logging in programing?

In computing, a log file is a file that records either events that occur in an operating system or other software runs, or messages between different users of communication software. Logging is the act of keeping a log. In the simplest case, messages are written to a single log file.

What are loggers in C#?

Logger is used for creating customized error log files or an error can be registered as a log entry in the Windows Event Log on the administrator's machine. This article is demonstrating how to create a text based error log file along with error messages with your own format, using a C# class.

What is a logging statement?

OVERVIEW. Logging statements are used to record valuable runtime information about applications. Each logging statement is assigned a log level such that users can disable some verbose log messages (e.g., “debug” messages) while allowing the printing of other important ones (e.g., “error” messages).


1 Answers

I use log4net in my own library but I would not do this if my library should be used by others (i.e. outside of my organization). In that case I would use an interface for logging, provide a default implementation based on log4net in separate assembly and use one way or another to allow the users of the library to inject their own implementation.

Having said this: I really like log4net and would not use anything else, but I think it is wrong to force others to have to use the logging framework of your choice.

Edit:

  • I would also not consider it good practice having the library log to a file by default. The users of your library should be able to decide where log messages end-up.
  • I would also provide a "no-operation" implementation that can be used if no logging at all is required. This probably should be default behavior an not require any additional assemblies or steps to implement.
like image 108
Stefan Egli Avatar answered Sep 28 '22 07:09

Stefan Egli