SaaS CMS has officially launched! Learn more now.

Tanuj
Feb 1, 2018
  4310
(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 London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024

Optimizely release SaaS CMS

Discover the future of content management with Optimizely SaaS CMS. Enjoy seamless updates, reduced costs, and enhanced flexibility for developers...

Andy Blyth | Jul 17, 2024 | Syndicated blog

A day in the life of an Optimizely Developer - London Meetup 2024

Hello and welcome to another instalment of A Day In The Life Of An Optimizely Developer. Last night (11th July 2024) I was excited to have attended...

Graham Carr | Jul 16, 2024

Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024