Serilog - Log entry Formatting

avistokar's Avatar

avistokar

11 Aug, 2017 02:56 PM

Using the LogAspect with Serilog includes the data I need as structured data and allow me to view/filter it using Seq. I would like the text logged to be shorted to not include the source= but continute to include the source in the structured data. So instead of :
MainViewModel..ctor(Microsoft.Practices.Unity.IUnityContainer = Microsoft.Practices.Unity.UnityContainer) | Succeeded, source={C:\Users\avistokar\Documents\Projects\OfficeAnywhere\OfficeAnywhere\Source\OfficeAnywhere.Office\ViewModels\MainViewModel.cs: line 64}.
I would like MainViewModel..ctor but continue to get the data passes to serilog as structured data. This would make the logs easier to peruse.

  1. Support Staff 1 Posted by PostSharp Techn... on 12 Aug, 2017 08:32 PM

    PostSharp Technologies's Avatar

    Hello,

    Normally Serilog extracts the properties from the text of the emitted log message. When you don't want to include the property in the message text, you can use another option to add properties - enrichment. This is documented on Serilog wiki (https://github.com/serilog/serilog/wiki/Enrichment) and there's a relevant discussion on StackOverflow (https://stackoverflow.com/questions/40160788/serilog-field-names-fo...).

    First you need to enable enrichment in the serilog configuration:

    new LoggerConfiguration()
        .Enrich.FromLogContext()
    

    Then you need to create a new PostSharp logging back-end and log record builder. The new classes should derive from SerilogLoggingBackend and SerilogLogRecordBuilder, and override a few methods as shown below.

    public class MySerilogLoggingBackend : SerilogLoggingBackend
    {
        public override LogRecordBuilder CreateRecordBuilder()
        {
            return new MySerilogLogRecordBuilder(this);
        }
    }
    
    public class MySerilogLogRecordBuilder : SerilogLogRecordBuilder
    {
        private IDisposable pushedPropertiesToken;
    
        public MySerilogLogRecordBuilder(MySerilogLoggingBackend backend) : base(backend)
        {
        }
    
        public override void BeginRecord(LoggingContext context, ref LogRecordInfo recordInfo, ref LogMemberInfo memberInfo)
        {
            base.BeginRecord(context, ref recordInfo, ref memberInfo);
    
            this.pushedPropertiesToken = LogContext.Push(
                new PropertyEnricher("SourceFile", this.SourceLineInfo.File),
                new PropertyEnricher("SourceLine", this.SourceLineInfo.Line));
        }
    
        protected override void EmitRecord()
        {
            base.EmitRecord();
    
            if (this.pushedPropertiesToken != null)
            {
                this.pushedPropertiesToken.Dispose();
                this.pushedPropertiesToken = null;
            }
        }
    
        protected override void Reset()
        {
            base.Reset();
    
            if (this.pushedPropertiesToken != null)
            {
                this.pushedPropertiesToken.Dispose();
                this.pushedPropertiesToken = null;
            }
        }
    
        protected override void AppendSourceLineInfo()
        {
            // Do not append source line info to the message text.
        }
    }
    

    Finally, configure PostSharp logging to use your custom back-end:

    LoggingServices.DefaultBackend = new MySerilogLoggingBackend();
    

    -alex

  2. Support Staff 2 Posted by PostSharp Techn... on 21 Aug, 2017 12:14 PM

    PostSharp Technologies's Avatar

    Hello,

    We are going to close this request as there have not been any further updates. Please feel free to reopen the discussion if you need more help.

    Thanks,
    PostSharp Team

  3. PostSharp Technologies closed this discussion on 21 Aug, 2017 12:14 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac