Try our conversational search powered by Generative AI!

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:>
        <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:

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: (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
Roll Your Own Security Headers

Proper security headers are a must for your Optimizely driven website. There are a variety of tools out there that will help with this, but when...

Ethan Schofer | Feb 21, 2024

Migrate Catalog content properties

A colleague asked me yesterday – how do we migrate properties of catalog content. There is, unfortunately, no official way to do it. There are...

Quan Mai | Feb 20, 2024 | Syndicated blog

Adjust log levels in Optimizely DXP

You may adjust the log levels for your site in Optimizely DXP yourself, but only for the Integration environment. Follow this step-by-step guide.

Tomas Hensrud Gulla | Feb 20, 2024 | Syndicated blog

Introducing Search & Navigation Dashboard for Resource Usage

We're excited to unveil the latest addition to the Search & Navigation suite: a dashboard designed to proactively monitor your resource usage. It's...

Edvin Dackelid Johansson | Feb 20, 2024