Try our conversational search powered by Generative AI!

Tanuj
Feb 1, 2018
  4242
(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" />
        </layout>
    </appender>
    <!--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" />
 </logger>

      

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.");

Preview: 

 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

Comments

valdis
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
Tanuj Feb 6, 2018 09:54 PM

Thank you, Guys! I really Appreciate your Input! 

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog