Feb 1, 2018
(3 votes)

Extending EPiServerLog.config

Log custom errors or success/fail information for REST APIs, Web services etc using EPiServer Log4net namespace [EPiServer.Logging]

Step 1: Add your custom Logging information to EpiserverLog.config

        a. Add your Appender

        Sample  File Appender:

    <!--Appender for MyCustomLog using File Appender -->
    <appender name="MyCustomLog" type="log4net.Appender.FileAppender">
        <!--File name and Location-->
        <file value="App_Data\MyCustomLog.log" />
        <!--Indicates whether the file should be appended to or overwritten.-->
        <appendToFile value="true" />
        <!--Encoding Type-->
        <encoding value="utf-8" />
        <!--true if always should be logged to the same file, otherwise false.-->
        <staticLogFileName value="true"/>
        <!--Log all information-->
        <threshold value="All" />
        <!--Types of Locking: https://logging.apache.org/log4net/log4net-1.2.13/release/sdk/log4net.Appender.FileAppender.LockingModel.html-->
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %message%newline" />
    <!--Appender for MyCustomLog data-->

    b. Add Logger tag right after </root> & before </log4net>


<!--Separate Logger for MyCustomLog -->
 <logger additivity="false" name="MyCustomLog">
        <level value="All"/>
        <!--Your Appender Name-->
        <appender-ref ref="MyCustomLog" />


Step 2: Write to your log


EPiServer.Logging.ILogger mylog = EPiServer.Logging.LogManager.Instance.GetLogger("MyCustomLog");
// Appender name: "MyCustomLog"

 mylog.Info($"This is test data.");


 Image 111.bmp



Different Appender types available:

  1. Write to File: [log4net.Appender.FileAppender] - Write to a file.
  2. Write To Rolling File: [log4net.Appender.RollingFileAppender] - Rolling Style used by EPiServer.
  3. Database log: [log4net.Appender.AdoNetAppender] - write logs to database.
  4. Write to Event log: [log4net.Appender.EventLogAppender] - Write events to event log.

More Info Here: https://world.episerver.com/documentation/developer-guides/CMS/logging/

Feb 01, 2018


valdis Feb 2, 2018 07:26 PM

next level would be to instruct IoC container (in composition root) to inject this logger with proper name automatically wherever you will need that.

Antti Alasvuo
Antti Alasvuo Feb 4, 2018 07:21 PM

Hi, I think you shouldn't be using the EPiServer.Logging.Compatibility for anything else than quickly changing existing direct log4net usage in existing code. It exists for backwards compatibility as stated in the documentation: https://world.episerver.com/documentation/developer-guides/CMS/logging/ (title on the page: Backwards compatibility)

All new Episerver projects and addons should be using the EPiServer.Logging namespace and the Episerver ILogger and LogManager implementations in that namespace.

There is one thing that I think is odd about the Episerver implementation as there is no static method to get a named logger but there is static method to get a logger by type. To get a named logger (based on string) you need to use instance method GetLogger(string) of LogManager. But there is the static instance property in LogManager.

To get the named logger you would have this line in your class:

private static readonly ILogger namedLogger = LogManager.Instance.GetLogger("MyNamedLogger");

As a side note, if you keep using log4net implementation directly you might endup in a situation where you get no logs depending are you using debug or release build. This is most likely caused by your code calling logging before Episerver and that means that log4net is not configured. Episerver log4net configuration is in EPiServer.Logging.Log4Net assembly, the assembly configuration attribute:  [assembly: XmlConfigurator(ConfigFile = "episerverlog.config", Watch = true)] and the log4net documentation states the first calling codes assembly is looked for this attribute => issue is that if you directly use log4net you by pass the configuration because the XmlConfigurator attribute is not in your assembly (well you can work around that by decorating your assembly with the same XmlConfigurator attribute, but mayce you shouldn't just use Episerver logging facade.

Antti Alasvuo
Antti Alasvuo Feb 4, 2018 07:32 PM

And naturally forgot to mention the obvious when using the Episerver LogManager to get loggers you are naturally "calling" from Episerver implementation and get the assembly attribute configuration properly initialized ;)

Tanuj Feb 6, 2018 09:54 PM

Thank you, Guys! I really Appreciate your Input! 

Please login to comment.
Latest blogs
Translating Optimizely CMS 12 UI components

Optimizely CMS 12 have been out for a while now, but still some elements haven't been properly translated resulting in a GUI defaulting to english....

Eric Herlitz | Jan 26, 2023 | Syndicated blog

Image preview in Optimizely CMS12 all properties view

With these simple steps, you can now see an Image and its Metadata, including size and dimensions, when editing an Image property in Optimizely...

Tomas Hensrud Gulla | Jan 26, 2023 | Syndicated blog

Setting up the ImageEditor in Optimizely CMS 12

Setting up certain configurations on Opimizely CMS 12 differs quite a bit from prior versions of (Episerver CMS 11 and older). Here's a small guide...

Eric Herlitz | Jan 25, 2023 | Syndicated blog

Happy Hour Returning in February

Hi everyone! It's been a while and we're excited to resume our Happy Hour in February for more learning, sharing, connecting, relaxing, and just to...

Patrick Lam | Jan 24, 2023