Skip to content

Instantly share code, notes, and snippets.

@johnkors
Created January 26, 2015 11:29
Show Gist options
  • Save johnkors/41fcae2cae565e01515d to your computer and use it in GitHub Desktop.
Save johnkors/41fcae2cae565e01515d to your computer and use it in GitHub Desktop.
IdentityServer v3 IEventService implementation using the Serilog ElasticSearch sink to provide messages on LogStash format
public class ElasticSearchEventService : IEventService
{
private readonly ElasticsearchSink _nativeSink;
public ElasticSearchEventService(IElasticSearchEventConfig conf)
{
var elasticsearchSinkOptions = new ElasticsearchSinkOptions(conf.ElasticSearchUri);
_nativeSink = new ElasticsearchSink(elasticsearchSinkOptions);
}
public void Raise<T>(Event<T> evt)
{
var properties = new List<LogEventProperty>
{
new LogEventProperty("Type", new ScalarValue("IdServerEvent")),
new LogEventProperty("Category", new ScalarValue(evt.Category)),
new LogEventProperty("ActivityId", new ScalarValue(evt.Context.ActivityId)),
new LogEventProperty("MachineName", new ScalarValue(evt.Context.MachineName)),
new LogEventProperty("ProcessId", new ScalarValue(evt.Context.ProcessId)),
new LogEventProperty("RemoteIpAddress", new ScalarValue(evt.Context.RemoteIpAddress)),
new LogEventProperty("SubjectId", new ScalarValue(evt.Context.SubjectId)),
new LogEventProperty("Details", new ScalarValue(evt.Details)),
new LogEventProperty("EventType", new ScalarValue(evt.EventType)),
new LogEventProperty("Id", new ScalarValue(evt.Id)),
new LogEventProperty("Message", new ScalarValue(evt.Message)),
new LogEventProperty("Name", new ScalarValue(evt.Name))
};
var messageTemplateTokens = new List<MessageTemplateToken>
{
new PropertyToken("message", evt.Message)
};
var messageTemplate = new MessageTemplate(evt.Message, messageTemplateTokens);
var nativeEvent = new LogEvent(evt.Context.TimeStamp, LogEventLevel.Information, null, messageTemplate, properties);
_nativeSink.Emit(nativeEvent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment