Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chris Waterhouse 5 posts 115 karma points
    Oct 14, 2020 @ 09:16
    Chris Waterhouse
    0

    Can't get Serilog Custom Sink Working

    Hello,

    We're in the process of upgrading from Umbraco 7 to 8, a part of this is obviously the log4net to Serilog change. Previously we used an appender to assist with our exception reporting and from the looks of the documentation this feature has been replaced by sinks in Serilog.

    But I can't for the life of me get the serilog config to register my custom sink, depending on how I try and configure the serilog.user.config file it either floats between throwing an error saying "The given assembly name or codebase was invalid" or by loading fine and never hitting the emit method inside the custom sink. Below is the custom sink cs file:

    namespace Moneyfacts.ConsumerSite.Utility
    {
        public class ExceptionReportingSink : ILogEventSink
        {
            IFormatProvider _formatProvider;
    
            public ExceptionReportingSink(IFormatProvider formatProvider)
            {
                _formatProvider = formatProvider;
            }
    
            public void Emit(LogEvent logEvent)
            {
                if (logEvent.Level == LogEventLevel.Error || logEvent.Level == LogEventLevel.Fatal)
                {
                    string serviceUrl = WebConfigurationManager.AppSettings["ExceptionsWebService"];
    
                    var exception = new
                    {
                        Message = logEvent.Exception.InnerException != null ? logEvent.Exception.InnerException.Message : logEvent.Exception.Message,
                        ServerName = ConfigurationManager.AppSettings.Get("serverName"),
                        Application = "Consumer Site",
                        Date = DateTime.Now,
                        Severity = "Error",
                    };
    
                    ReportException(exception, serviceUrl);
                }
            }
    
            public void ReportException(object exception, string serviceUrl)
            {
                var request = WebRequest.Create(serviceUrl + "/exceptions/LogException");
                request.Method = "POST";
                request.ContentType = "application/json";
                byte[] byteArray = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(exception));
                request.ContentLength = byteArray.Length;
    
                //don't wrap this in try/catch - it will be picked up at application level anyway
                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Close();
                }
                request.GetResponseAsync();
            }
        }
    }
    

    I've lost track of the amount of serilog.user.config options that i've tried to implement to get it working,

    in various different combinations i've tried the following and none of them have worked successfully (Umbraco 8 is the temporary project name):

    <add key="serilog:using:ExceptionReportingSink" value="Moneyfacts.Utility.ExceptionReportingSink, Umbraco8" />
    <add key="serilog:write-to:ExceptionReportingSink" value="Moneyfacts.ConsumerSite.Utility.ExceptionReportingSink, Umbraco8" />
    <add key="serilog:write-to:ExceptionReportingSink" value="Moneyfacts.ConsumerSite.Utility.ExceptionReportingSink" />
    <add key="serilog:write-to:ExceptionReportingSink" />
    

    I tried implementing the Serilog.Sinks.Http package as an alternative solution but I couldn't get it to use that either. If I stick the sample code for "Writing your own log messages to a custom file" from https://our.umbraco.com/documentation/reference/config/Serilog/

    into the serilog.user.config file then it picks up the changes fine and logs to the correct place.

    Can anyone point out where i'm going wrong/what else I need to do?

    Thanks in advance

  • Chris Waterhouse 5 posts 115 karma points
    Oct 14, 2020 @ 09:55
    Chris Waterhouse
    100

    I've got it working when not using the serilog.user.config file with the following:

    namespace Moneyfacts.ConsumerSite
    {
        public static class LogSinkExtensions
        {
            public static LoggerConfiguration AddExceptionReportingSink(this LoggerSinkConfiguration loggerConfiguration, IFormatProvider formatProvider = null)
            {
                return loggerConfiguration.Sink(new ExceptionReportingSink(formatProvider));
            }
        }
    }
    

    and in the startup file overwriting the logger with the following:

            Log.Logger = new LoggerConfiguration().WriteTo.AddExceptionReportingSink().CreateLogger();
    

    But is there a way to get it working with the serilog.user.config file?

Please Sign in or register to post replies

Write your reply to:

Draft