/// <summary> /// Adds a non durable sink that sends log events using HTTP POST over the network. A /// non-durable sink will lose data after a system or process restart. /// </summary> /// <param name="sinkConfiguration">The logger configuration.</param> /// <param name="requestUri">The URI the request is sent to.</param> /// <param name="batchPostingLimit"> /// The maximum number of events to post in a single batch. Default value is 1000. /// </param> /// <param name="period"> /// The time to wait between checking for event batches. Default value is 2 seconds. /// </param> /// <param name="textFormatter"> /// The formatter rendering individual log events into text, for example JSON. Default /// value is <see cref="NormalRenderedTextFormatter"/>. /// </param> /// <param name="batchFormatter"> /// The formatter batching multiple log events into a payload that can be sent over the /// network. Default value is <see cref="DefaultBatchFormatter"/>. /// </param> /// <param name="restrictedToMinimumLevel"> /// The minimum level for events passed through the sink. Default value is /// <see cref="LevelAlias.Minimum"/>. /// </param> /// <param name="httpClient"> /// A custom <see cref="IHttpClient"/> implementation. Default value is /// <see cref="HttpClient"/>. /// </param> /// <returns>Logger configuration, allowing configuration to continue.</returns> public static LoggerConfiguration Http( this LoggerSinkConfiguration sinkConfiguration, string requestUri, int batchPostingLimit = 1000, TimeSpan?period = null, ITextFormatter textFormatter = null, IBatchFormatter batchFormatter = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, IHttpClient httpClient = null) { if (sinkConfiguration == null) { throw new ArgumentNullException(nameof(sinkConfiguration)); } var sink = new HttpSink( requestUri, batchPostingLimit, period ?? TimeSpan.FromSeconds(2), textFormatter ?? new NormalRenderedTextFormatter(), batchFormatter ?? new DefaultBatchFormatter(), httpClient ?? new HttpClientWrapper()); return(sinkConfiguration.Sink(sink, restrictedToMinimumLevel)); }
public HttpSinkTest() { client = new Mock <IHttpClient>(); requestUri = "www.mylogs.com"; sink = new HttpSink( client.Object, requestUri, HttpSink.DefaultBatchPostingLimit, HttpSink.DefaultPeriod, null); }
/// <summary> /// Adds a non-durable sink that sends log events using HTTP POST over the network. The log /// events are stored in memory in the case that the log server cannot be reached. /// <para /> /// The maximum number of log events stored in memory is configurable, and given that we /// reach this limit the sink will drop new log events in favor of keeping the old. /// <para /> /// A non-durable sink will lose data after a system or process restart. /// </summary> /// <param name="sinkConfiguration">The logger configuration.</param> /// <param name="requestUri">The URI the request is sent to.</param> /// <param name="queueLimitBytes"> /// The maximum size, in bytes, of events stored in memory, waiting to be sent over the /// network. Specify null for no limit. /// </param> /// <param name="logEventLimitBytes"> /// The maximum size, in bytes, for a serialized representation of a log event. Log events /// exceeding this size will be dropped. Specify null for no limit. Default value is null. /// </param> /// <param name="logEventsInBatchLimit"> /// The maximum number of log events sent as a single batch over the network. Default /// value is 1000. /// </param> /// <param name="batchSizeLimitBytes"> /// The approximate maximum size, in bytes, for a single batch. The value is an /// approximation because only the size of the log events are considered. The extra /// characters added by the batch formatter, where the sequence of serialized log events /// are transformed into a payload, are not considered. Please make sure to accommodate for /// those. /// <para /> /// Another thing to mention is that although the sink does its best to optimize for this /// limit, if you decide to use an implementation of <seealso cref="IHttpClient"/> that is /// compressing the payload, e.g. <seealso cref="JsonGzipHttpClient"/>, this parameter /// describes the uncompressed size of the log events. The compressed size might be /// significantly smaller depending on the compression algorithm and the repetitiveness of /// the log events. /// <para /> /// Default value is null. /// </param> /// <param name="period"> /// The time to wait between checking for event batches. Default value is 2 seconds. /// </param> /// <param name="textFormatter"> /// The formatter rendering individual log events into text, for example JSON. Default /// value is <see cref="NormalRenderedTextFormatter"/>. /// </param> /// <param name="batchFormatter"> /// The formatter batching multiple log events into a payload that can be sent over the /// network. Default value is <see cref="ArrayBatchFormatter"/>. /// </param> /// <param name="restrictedToMinimumLevel"> /// The minimum level for events passed through the sink. Default value is /// <see cref="LevelAlias.Minimum"/>. /// </param> /// <param name="httpClient"> /// A custom <see cref="IHttpClient"/> implementation. Default value is /// <see cref="JsonHttpClient"/>. /// </param> /// <param name="configuration"> /// Configuration passed to <paramref name="httpClient"/>. Parameter is either manually /// specified when configuring the sink in source code or automatically passed in when /// configuring the sink using /// <see href="https://www.nuget.org/packages/Serilog.Settings.Configuration">Serilog.Settings.Configuration</see>. /// </param> /// <returns>Logger configuration, allowing configuration to continue.</returns> public static LoggerConfiguration Http( this LoggerSinkConfiguration sinkConfiguration, string requestUri, long?queueLimitBytes, long?logEventLimitBytes = null, int?logEventsInBatchLimit = 1000, long?batchSizeLimitBytes = null, TimeSpan?period = null, ITextFormatter?textFormatter = null, IBatchFormatter?batchFormatter = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, IHttpClient?httpClient = null, IConfiguration?configuration = null) { if (sinkConfiguration == null) { throw new ArgumentNullException(nameof(sinkConfiguration)); } if (requestUri == null) { throw new ArgumentNullException(nameof(requestUri)); } // Default values period ??= TimeSpan.FromSeconds(2); textFormatter ??= new NormalRenderedTextFormatter(); batchFormatter ??= new ArrayBatchFormatter(); httpClient ??= new JsonHttpClient(); if (configuration != null) { httpClient.Configure(configuration); } var sink = new HttpSink( requestUri: requestUri, queueLimitBytes: queueLimitBytes, logEventLimitBytes: logEventLimitBytes, logEventsInBatchLimit: logEventsInBatchLimit, batchSizeLimitBytes: batchSizeLimitBytes, period: period.Value, textFormatter: textFormatter, batchFormatter: batchFormatter, httpClient: httpClient); return(sinkConfiguration.Sink(sink, restrictedToMinimumLevel)); }
/// <summary> /// Adds a sink that sends log events using HTTP POST over the network. /// </summary> /// <param name="sinkConfiguration">The logger configuration.</param> /// <param name="requestUri">The URI the request is sent to.</param> /// <param name="batchPostingLimit"> /// The maximum number of events to post in a single batch. The default is /// <see cref="HttpSink.DefaultBatchPostingLimit"/>. /// </param> /// <param name="period"> /// The time to wait between checking for event batches. The default is /// <see cref="HttpSink.DefaultPeriod"/>. /// </param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="restrictedToMinimumLevel"> /// The minimum level for events passed through the sink. The default is /// <see cref="LevelAlias.Minimum"/>. /// </param> /// <returns>Logger configuration, allowing configuration to continue.</returns> public static LoggerConfiguration Http( this LoggerSinkConfiguration sinkConfiguration, string requestUri, int?batchPostingLimit = null, TimeSpan?period = null, IFormatProvider formatProvider = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum) { if (sinkConfiguration == null) { throw new ArgumentNullException(nameof(sinkConfiguration)); } var client = new HttpClientWrapper(); var sink = new HttpSink( client, requestUri, batchPostingLimit ?? HttpSink.DefaultBatchPostingLimit, period ?? HttpSink.DefaultPeriod, formatProvider); return(sinkConfiguration.Sink(sink, restrictedToMinimumLevel)); }
/// <summary> /// Adds a non-durable sink that sends log events using HTTP POST over the network. A /// non-durable sink will lose data after a system or process restart. /// </summary> /// <param name="sinkConfiguration">The logger configuration.</param> /// <param name="requestUri">The URI the request is sent to.</param> /// <param name="batchPostingLimit"> /// The maximum number of events to post in a single batch. Default value is 1000. /// </param> /// <param name="batchSizeLimitBytes"> /// The approximate maximum size, in bytes, for a single batch. The value is an /// approximation because only the size of the log events are considered. The extra /// characters added by the batch formatter, where the sequence of serialized log events /// are transformed into a payload, are not considered. Please make sure to accommodate for /// those. Default value is long.MaxValue. /// </param> /// <param name="queueLimit"> /// The maximum number of events stored in the queue in memory, waiting to be posted over /// the network. Default value is infinitely. /// </param> /// <param name="period"> /// The time to wait between checking for event batches. Default value is 2 seconds. /// </param> /// <param name="textFormatter"> /// The formatter rendering individual log events into text, for example JSON. Default /// value is <see cref="NormalRenderedTextFormatter"/>. /// </param> /// <param name="batchFormatter"> /// The formatter batching multiple log events into a payload that can be sent over the /// network. Default value is <see cref="DefaultBatchFormatter"/>. /// </param> /// <param name="restrictedToMinimumLevel"> /// The minimum level for events passed through the sink. Default value is /// <see cref="LevelAlias.Minimum"/>. /// </param> /// <param name="httpClient"> /// A custom <see cref="IHttpClient"/> implementation. Default value is /// <see cref="HttpClient"/>. /// </param> /// <param name="configuration"> /// Configuration passed to <paramref name="httpClient"/>. Parameter is either manually /// specified when configuring the sink in source code or automatically passed in when /// configuring the sink using /// <see href="https://www.nuget.org/packages/Serilog.Settings.Configuration">Serilog.Settings.Configuration</see>. /// </param> /// <returns>Logger configuration, allowing configuration to continue.</returns> public static LoggerConfiguration Http( this LoggerSinkConfiguration sinkConfiguration, string requestUri, int batchPostingLimit = 1000, long batchSizeLimitBytes = long.MaxValue, int?queueLimit = null, TimeSpan?period = null, ITextFormatter textFormatter = null, IBatchFormatter batchFormatter = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, IHttpClient httpClient = null, IConfiguration configuration = null) { if (sinkConfiguration == null) { throw new ArgumentNullException(nameof(sinkConfiguration)); } // Default values period ??= TimeSpan.FromSeconds(2); textFormatter ??= new NormalRenderedTextFormatter(); batchFormatter ??= new DefaultBatchFormatter(); httpClient ??= new DefaultHttpClient(); httpClient.Configure(configuration); var sink = new HttpSink( requestUri: requestUri, batchPostingLimit: batchPostingLimit, batchSizeLimitBytes: batchSizeLimitBytes, queueLimit: queueLimit, period: period.Value, textFormatter: textFormatter, batchFormatter: batchFormatter, httpClient: httpClient); return(sinkConfiguration.Sink(sink, restrictedToMinimumLevel)); }
public HttpSinkReflection(Logger logger) { sink = logger.GetSink <HttpSink>(); }