I have created a custom log appender. This is from the default Sitecore.Logging.dll
.
public class ApiLogger : log4net.Appender.ForwardingAppender
{
public ApiLogger()
{
LogProperties = new List<KeyValuePair<string, string>>();
}
public ApiLogger(List<KeyValuePair<string, string>> logProperties)
{
LogProperties = logProperties;
}
public List<KeyValuePair<string, string>> LogProperties { get; set; }
protected override void Append(LoggingEvent loggingEvent)
{
///Send log to 3rd Party API
}
}
and declared it like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:security="http://www.sitecore.net/xmlconfig/security/">
<sitecore>
<log4net>
<appender name="SeqLogger" type="MyApp.ApiLogger, MyApp" patch:after = "appender[@name='LogFileAppender']">
<layout type="log4net.Layout.PatternLayout" />
<encoding value="utf-8" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ApiLogger" patch:after = "*[@ref='LogFileAppender']"/>
</root>
</log4net>
</sitecore>
</configuration>
This is the calling method
public ActionResult UpdateOrder()
{
try
{
///update order
}
catch(Exception ex)
{
Log.Error("", ex, this);
}
}
When there is an error, the default Sitecore log.txt is created and after that, the Append()
method is triggered, where I'm sending the log info to a 3rd party API.
This custom logging is working fine & I'm able to view the exception details in the 3rd Party portal.
However, on an error, I would like to also capture the "Order" details like Id, Name etc.
The LogProperties
was created for this but don't know how to use it.
The reason, I'm not calling the API directly in the UpdateOrder()
method is, there are several other methods in the solution which have their respective details to be captured.
One approach I could think of is, passing a JSON string as the first parameter in the Log.Error()
method, but then it's not possible to know if that string is a collection of properties or any custom message.
Is there a way to add the custom data to the LoggingEvent
in the caller method itself, or any better way to achieve what I'm trying to.