EPiWiki.se  - EPiServer notes shared with others
 

Create a log message programmatically

[Edit]
How to use EPiServers CMS log functionality to create your own log messages.

Create a logger


Start adding log4net as a reference for yor project.

using log4net;

public class MyClass
{
   private static readonly ILog _log = LogManager.GetLogger(typeof(MyClass));

Write a debug message to the log



if (_log.IsDebugEnabled)
{
   _log.Debug("Test logger");
}

Design tips: Always check if the logger is enabled before doing a lot of stuff.
Otherwise the debug code is going to slow your application down even if it’s not enabled.

Write an error message to the log



try
{
   ...
}
catch (MyException ex)
{
   _log.Error("Can't execute test code, because", ex);
}

Design tips: Never leave a catch block empty, if the exception is handled some
other way please write a comment about this in the block.
Version author:
Mattias Lövström

EPiServer version

All