Showing posts with label log4net. Show all posts
Showing posts with label log4net. Show all posts

Wednesday, January 4, 2017

Sitecore Log4Net Appender for Slack


    Slack became essential part of almost all projects(or teams). It is very powerful communication tool that speed up sharing information. As we use Slack for our Sitecore-based project I decided to create prototype of slack bot that will share information about Sitecore production site health to Slack channel. 


    After quick search I found that something similar was already created on Sitecore Hackathon, named Slack for Sitecore. There is also video that describe how it works.

    But as for me, it is too complex to use it in real projects. You don't need to have a lot of information in Slack channel, otherwise you will skip all messages from Slack bot. And also you don't need to spend a time on configuration of module. Sitecore already has mechanism how to show that something went wrong - logging errors to log file. Why don't use it for our Slack bot?

    First of all, we should take Slack log4net appender and change it to be suitable for Sitecore. It could be done quite easily by changing dependency from log4net assembly to Sitecore.Logging assembly.

    Now, we are able to add log4slack appender to our Sitecore configuration:

<appender name="SlackAppender"
    type="Log4Slack.SlackAppender, Log4Slack">
  <WebhookUrl value="https://hooks.slack.com/**********************" />
  <Channel value="#testing" />
  <Username value="*********" />
  <IconUrl value="" />
  <IconEmoji value=":ghost:" />
  <AddAttachment value="true" />
  <AddExceptionTraceField value="true" />
  <UsernameAppendLoggerName value="true"/>
  <threshold value="Error" />
</appender>
....
<root>
  <priority value="INFO"/>
  <appender-ref ref="LogFileAppender"/>
  <appender-ref ref="SlackAppender"/>
</root>

   And we got next messages in our Slack channel:

    Of course, you don't need having all logs to be transferred to your Slack channel, because no one will read them. That is why you either should set up separate logger for critical messages or set logging level for this appender to error: <threshold value="Error" />. And you will not miss any critical exception on your site.

Wednesday, December 28, 2016

Extension of Sitecore Log Abilities

    Sitecore have an ample capabilities on logging. All logging system is based on log4net services. And if you need to change destination of your logs all that you need – configure right log appender. There is wide range of log4net appenders available out of the box. And also big amount of open source modules that you are able to fit for your needs. For example:
  1. You can write your logs in MongoDB
  2. You can create Slack bot that will put logs in Slack channel. 
  3. Use ElasticSearch+LogStash+Kibana(or RabbitMQ) 
     
    But there are few peculiar properties that doesn’t allow to use major appenders out of the box. Log4net web.config configuration section in Sitecore is initialized by Sitecore.Logging (not by log4net.dll) assembly and it requres additional steps. Sitecore.Logging is extended copy of log4net assembly. Even namespaces are same! While fitting new appender to Sitecore your appender should be inherited from log4net.Appender.AppenderSkeleton. Take into account that class with same namespace is present in both log4net.dll and Sitecore.Logging.dll. You need to use class from Sitecore.Logging.dll !!!

To summarize, list of steps of fitting new appender to Sitecore will be next:
  1. Get sources of appender 
  2. Add reference to Sitecore.Logging assembly with alias Sitecore for ability to use log4net and Sitecore.Logging in same project 
  3. Inherit your appender from Sitecore.Logging AppenderSkeleton class implementation MyAppender : Sitecore::log4net.Appender.AppenderSkeleton 
  4. Implement missed class members: you need to reimplement Append method. It has different signature comparing to default one Append(Sitecore::log4net.spi.LoggingEvent loggingEvent)