SitecoreSerilog 0.0.5.2-preview

This is a prerelease version of SitecoreSerilog.
There is a newer version of this package available.
See the version list below for details.
dotnet add package SitecoreSerilog --version 0.0.5.2-preview
                    
NuGet\Install-Package SitecoreSerilog -Version 0.0.5.2-preview
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SitecoreSerilog" Version="0.0.5.2-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SitecoreSerilog" Version="0.0.5.2-preview" />
                    
Directory.Packages.props
<PackageReference Include="SitecoreSerilog" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SitecoreSerilog --version 0.0.5.2-preview
                    
#r "nuget: SitecoreSerilog, 0.0.5.2-preview"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SitecoreSerilog@0.0.5.2-preview
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SitecoreSerilog&version=0.0.5.2-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=SitecoreSerilog&version=0.0.5.2-preview&prerelease
                    
Install as a Cake Tool

Sitecore Serilog

GitHub Actions Workflow Status

NuGet Version NuGet Downloads

Provides Serilog appender for Sitecore.

Features

  • Serilog integration in Sitecore
  • Use any Sink from Serilog with Sitecore
  • Custom enricher for Sitecore context (like database, context item, page item, rendering, datasource, etc) in log event (with option to extend it)
  • Custom HTTP context enricher (currently HTTP method and URL are logged)
  • Add your custom enrichers

SEQ Example

You can see how your log event with exception details could look like (in SEQ):

seq example 1 seq example 2

Usage

See examples project for more details.

Appender

Create a custom appender:

namespace SitecoreSerilog.Example
{
    public class BaseSeqSitecoreAppender : BaseSitecoreSerilogAppender
    {
        public string ApiKey { get; set; }
        public string SeqHost { get; set; }

        // configure Enrichers here (use listed below or define your own enrichers):
        protected override LoggerConfiguration Enrich(LoggerConfiguration configuration)
        {
            return configuration
                    // application name enricher (so you know, which site is used)
                    .Enrich.WithApplicationName(() => Sitecore.Context.Site?.Name ?? "FallbackValue")
                    // spoke name enricher (so you know, which environment is used)
                    .Enrich.WithSpokeName(Settings.GetSetting("SpokeName"))
                    .Enrich.FromLogContext()
                    .Enrich.WithUtcTimestamp()
                    .Enrich.WithMachineName()
                    .Enrich.WithEnvironmentUserName()
                    .Enrich.WithProcessId()
                    .Enrich.WithProcessName()
                    .Enrich.WithThreadId()
                    .Enrich.WithMemoryUsage()
                    // custom SitecoreContextEnricher - so you can see the Sitecore Context Details in log event
                    .Enrich.WithSitecoreContext((options) => { options.MinLevel = LogEventLevel.Error; })
                    // custom HttpContextEnricher - so you can see HttpContext details in log event
                    .Enrich.WithHttpContext(LogEventLevel.Error)
                ;
        }

        // configure where Serilog should write
        protected override LoggerConfiguration WriteTo(LoggerConfiguration configuration)
        {
            return configuration
                // in this example I write to SEQ, but you can use any Sink you need
                .WriteTo.Seq(SeqHost, apiKey: ApiKey);
        }

        // validate your configuration (if returned false - Serilog won't write anything)
        protected override bool ValidateConfiguration()
        {
            return !string.IsNullOrEmpty(SeqHost);
        }
    }
}

Configuration:

Use your appender:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
               xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore>
        <log4net>
            
            <appender name="SerilogAppender"
                      type="SitecoreSerilog.Example.BaseSeqSitecoreAppender, SitecoreSerilog.Example"
                      patch:after="appender[@name='LogFileAppender']">
                <minimumlevel value="INFO" />
                <apikey value="$(env:SEQ_API_KEY)" />
                <seqhost value="$(env:SEQ_URL)" />
                <layout type="log4net.Layout.PatternLayout" />
                <encoding value="utf-8" />
            </appender>

            
            <root>
                <appender-ref ref="SerilogAppender" patch:after="*[@ref='LogFileAppender']" />
            </root>
            
            
            <logger name="Sitecore.Diagnostics.Crawling">
                <appender-ref ref="SerilogAppender" patch:after="*[@ref='CrawlingLogFileAppender']" />
            </logger>
        </log4net>
    </sitecore>
</configuration>

Enrichers

SitecoreContextEnricher

Default usage
.Enrich.WithSitecoreContext(LogEventLevel.Error)
Advanced usage

E.g. add additional items to Logger:

.Enrich.WithSitecoreContext(options =>
    {
        // configure min log level: 
        options.MinLevel = LogEventLevel.Error;
        // extend default enrich object:
        options
            // add current Sitecore MVC page context
            .WithItem("PageItem", () => PageContext.CurrentOrNull?.Item)
            // add current rendering datasource
            .WithItem("Datasource", () => RenderingContext.CurrentOrNull?.Rendering?.Item)
            .WithContextOption(Constants.OptionNames.Context,
                context =>
                {
                    // add current RenderingId
                    context.WithProperty("RenderingId",
                        () => RenderingContext.CurrentOrNull?.Rendering?.Id.ToString());
                })
            ;
    })

HttpContextEnricher

Default usage
.Enrich.WithHttpContext(LogEventLevel.Error)

Contribute / Bug / Help

You can contact me via email or in Sitecore Slack: zharikovigor97@gmail.com. Or please open an issue.

Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 is compatible.  net472 was computed.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.1 10,805 6/11/2024
0.1.0 110 6/11/2024
0.0.5.3-preview 97 6/11/2024
0.0.5.2-preview 94 6/11/2024
0.0.5.1-preview 103 6/7/2024
0.0.5-preview 98 6/7/2024
0.0.4-preview 95 6/4/2024
0.0.3-preview 99 5/30/2024
0.0.2-preview 93 5/30/2024
0.0.1-preview 98 5/30/2024