This project has been merged into SumoLogic .NET appenders repository!
W4k.Serilog.Sinks.SumoLogic has been merged to SumoLogic/sumologic-net-appenders.
From now on, reference SumoLogic.Logging.Serilog package instead.
In order to follow SumoLogic terminology, one change was made when naming appenders. To migrate, rename sinks accordingly:
| W4k.Serilog.Sinks.SumoLogic sink name | SumoLogic.Logging.Serilog sink name |
|---|---|
| SumoLogicUnbufferedSink | SumoLogicSink |
| SumoLogicSink | BufferedSumoLogicSink |
(So as you can see, in SumoLogic package, buffered sink is named explicitly)
Serilog sink for logging events into SumoLogic.
Code is inspired by Bill Pratt's Serilog.Sinks.SumoLogic (billpratt/serilog-sinks-sumologic)
and SumoLogic own appenders for log4net and NLog (SumoLogic/sumologic-net-appenders).
Install-Package Serilog
Install-Package W4k.Serilog.Sinks.SumoLogicThere are two Serilog sinks included in this package:
SumoLogicSink: Uses buffer to collect events, which are being sent in batches (extension method:SumoLogic)SumoLogicUnbufferedSink: Sends event right away (extension method:SumoLogicUnbuffered)
To set up logger using configuration file, additional dependency is required - install Serilog.Settings.Configuration as well.
Install-Package Serilog.Settings.Configuration
Add appsettings.json (or whatever configuration you need) as follows:
{
"Serilog": {
"WriteTo": [
{
"Name": "SumoLogic",
"Args": {
"endpointUrl": "https://localhost",
"sourceName": "w4k-serilog-sumologic"
}
}
]
}
}
Notice property Name - either use SumoLogic or SumoLogicUnbuffered. Detailed description
of configuration properties are listed in Configuration section.
To load such configuration, use Serilog's ReadFrom method like this:
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Logger log = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();It is possible set up logger using extension method as well:
// alternatively .WriteTo.SumoLogicUnbuffered(...)
Logger log = new LoggerConfiguration()
.WriteTo.SumoLogic(new Uri("http://localhost"), sourceName: "w4k-serilog-sumologic")
.CreateLogger();When logging to SumoLogic, you may find useful to log JSON instead of plain text message. It is possible
to configure JSON formatter (Serilog.Formatting.Compact.CompactJsonFormatter) following way.
First install package containing JSON formatter:
Install-Package Serilog.Formatting.Compact
Then use extension method to assign new formatter to formatter argument:
new LoggerConfiguration().
.WriteTo.SumoLogic(new Uri("http://localhost"), formatter: new CompactJsonFormatter())
.CreateLogger();Alternatively, you can configure formatter in config file (see Configuration section):
{
"Serilog": {
"WriteTo": [
{
"Name": "SumoLogic",
"Args": {
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
"endpointUrl": "https://localhost",
}
}
]
}
}Either use as sink arguments in configuration or as name arguments of extension methods.
| Argument (B/*) | Description | Default value |
|---|---|---|
| endpointUrl | SumoLogic endpoint URL, mandatory | null |
| outputTemplate | A message template describing the format used to write to the sink | see below |
| sourceName | The name used for messages sent to SumoLogic server | null |
| sourceCategory | The source category for messages sent to SumoLogic server | null |
| sourceHost | The source host for messages sent to SumoLogic Server | null |
| clientName | The client name value that is included in each request (used for telemetry) | null |
| connectionTimeout | The connection timeout, in milliseconds | 60 000 |
| retryInterval (B) | The send message retry interval, in milliseconds | 10 000 |
| maxFlushInterval (B) | The maximum interval between flushes, in milliseconds | 10 000 |
| flushingAccuracy (B) | How often the messages queue is checked for messages to send, in milliseconds | 250 |
| messagesPerRequest (B) | How many messages need to be in the queue before flushing | 100 |
| maxQueueSizeBytes (B) | The messages queue capacity, in bytes | 1 000 000 |
| httpMessageHandler | Override HTTP message handler which manages requests to SumoLogic | null |
| formatter | Controls the rendering of log events into text, for example to log JSON | null |
| levelSwitch | A switch allowing the pass-through minimum level to be changed at runtime | null |
| restrictedToMinimumLevel | The minimum level for events passed through the sink, ignored if levelSwitch is set |
LevelAlias.Minimum |
arguments marked with "(B)" are available only to buffered sink (SumoLogicSink)
- default
outputTemplate="{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {Exception}", seeDefaultOutputTemplate - provide
HttpMessageHandler httpMessageHandlerto adjust HTTP request sent SumoLogic - when
LoggingLevelSwitch levelSwitchis set,LogEventLevel restrictedToMinimumLevelargument is ignored - when setting text formatter (
ITextFormatter formatter), always use fully qualified type name (if set,string outputTemplateis ignored)