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
Identify Azure web app instance id's for an Optimizely CMS site

When running Optimizely CMS in Azure, you will be using an instance bound cloud license. What instances are counted, and how can you check them? Le...

Tomas Hensrud Gulla | Feb 27, 2024 | Syndicated blog

Introducing Image Transformer - AI Assistant for Optimizely

We've got something super cool to share with you, and it's all about giving your images a fresh spin. Image Transformer, the latest feature from ou...

Luc Gosso (MVP) | Feb 26, 2024 | Syndicated blog

Welcome 2024 Winter OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Feb 26, 2024

Optimizely Opal... what it does actually do?

At Opticon 2023, Optimizely announced its first AI product Opal. AI is definitely the new tech buzzword in 2024 and with promises that AI will be...

Jon Jones | Feb 25, 2024 | Syndicated blog