If we want to log our own information using the log4net framework, we need to create a logger. Creating a logger of our own is actually quite simple.
Once log4net is configured, we simply call LogManager.GetLogger(<type>)
and pass in the class type we are working with. For example, if we wanted to log the creation of a new Address
, we would simply call GetLogger()
to get a log object and then call one of the logging methods such as Info()
.
private static ILog log = LogManager.GetLogger(typeof(Address)); ... log.InfoFormat("New Address Created: {0}", Address.Id);
This example uses the InfoFormat()
method. Most of the logging levels (DEBUG
, INFO
, WARN
, ERROR
, and FATAL
) have these Format()
methods, which take multiple arguments (in the format of the params
parameter list) that you can use to render logs containing contextual information. These methods use the same constructs as the string.Format()
method.