forked from serilog/serilog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request serilog#826 from nblumhardt/f-audit
Audit-style logging
- Loading branch information
Showing
13 changed files
with
369 additions
and
38 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/Serilog/Configuration/LoggerAuditSinkConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Configuration | ||
{ | ||
/// <summary> | ||
/// Controls sink configuration. | ||
/// </summary> | ||
public class LoggerAuditSinkConfiguration | ||
{ | ||
LoggerSinkConfiguration _sinkConfiguration; | ||
|
||
internal LoggerAuditSinkConfiguration(LoggerConfiguration loggerConfiguration, Action<ILogEventSink> addSink, Action<LoggerConfiguration> applyInheritedConfiguration) | ||
{ | ||
_sinkConfiguration = new LoggerSinkConfiguration(loggerConfiguration, addSink, applyInheritedConfiguration); | ||
} | ||
|
||
/// <summary> | ||
/// Audit log events to the specified <see cref="ILogEventSink"/>. | ||
/// </summary> | ||
/// <param name="logEventSink">The sink.</param> | ||
/// <param name="restrictedToMinimumLevel">The minimum level for | ||
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param> | ||
/// <param name="levelSwitch">A switch allowing the pass-through minimum level | ||
/// to be changed at runtime.</param> | ||
/// <returns>Configuration object allowing method chaining.</returns> | ||
public LoggerConfiguration Sink( | ||
ILogEventSink logEventSink, | ||
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, | ||
// ReSharper disable once MethodOverloadWithOptionalParameter | ||
LoggingLevelSwitch levelSwitch = null) | ||
{ | ||
return _sinkConfiguration.Sink(logEventSink, restrictedToMinimumLevel, levelSwitch); | ||
} | ||
|
||
/// <summary> | ||
/// Audit log events to the specified <see cref="ILogEventSink"/>. | ||
/// </summary> | ||
/// <typeparam name="TSink">The sink.</typeparam> | ||
/// <param name="restrictedToMinimumLevel">The minimum level for | ||
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param> | ||
/// <param name="levelSwitch">A switch allowing the pass-through minimum level | ||
/// to be changed at runtime.</param> | ||
/// <returns>Configuration object allowing method chaining.</returns> | ||
public LoggerConfiguration Sink<TSink>( | ||
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, | ||
LoggingLevelSwitch levelSwitch = null) | ||
where TSink : ILogEventSink, new() | ||
{ | ||
return _sinkConfiguration.Sink<TSink>(restrictedToMinimumLevel, levelSwitch); | ||
} | ||
|
||
/// <summary> | ||
/// Audit log events to a sub-logger, where further processing may occur. Events through | ||
/// the sub-logger will be constrained by filters and enriched by enrichers that are | ||
/// active in the parent. A sub-logger cannot be used to log at a more verbose level, but | ||
/// a less verbose level is possible. | ||
/// </summary> | ||
/// <param name="configureLogger">An action that configures the sub-logger.</param> | ||
/// <param name="restrictedToMinimumLevel">The minimum level for | ||
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param> | ||
/// <param name="levelSwitch">A switch allowing the pass-through minimum level | ||
/// to be changed at runtime.</param> | ||
/// <returns>Configuration object allowing method chaining.</returns> | ||
public LoggerConfiguration Logger( | ||
Action<LoggerConfiguration> configureLogger, | ||
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, | ||
LoggingLevelSwitch levelSwitch = null) | ||
{ | ||
return _sinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch); | ||
} | ||
|
||
/// <summary> | ||
/// Audit log events to a sub-logger, where further processing may occur. Events through | ||
/// the sub-logger will be constrained by filters and enriched by enrichers that are | ||
/// active in the parent. A sub-logger cannot be used to log at a more verbose level, but | ||
/// a less verbose level is possible. | ||
/// </summary> | ||
/// <param name="logger">The sub-logger. This will <em>not</em> be shut down automatically when the | ||
/// parent logger is disposed.</param> | ||
/// <param name="restrictedToMinimumLevel">The minimum level for | ||
/// events passed through the sink.</param> | ||
/// <returns>Configuration object allowing method chaining.</returns> | ||
public LoggerConfiguration Logger( | ||
ILogger logger, | ||
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum) | ||
{ | ||
return _sinkConfiguration.Logger(logger, restrictedToMinimumLevel); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Serilog.Debugging; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Core.Sinks | ||
{ | ||
class AggregateSink : ILogEventSink | ||
{ | ||
readonly ILogEventSink[] _sinks; | ||
|
||
public AggregateSink(IEnumerable<ILogEventSink> sinks) | ||
{ | ||
if (sinks == null) throw new ArgumentNullException(nameof(sinks)); | ||
_sinks = sinks.ToArray(); | ||
} | ||
|
||
public void Emit(LogEvent logEvent) | ||
{ | ||
List<Exception> exceptions = null; | ||
foreach (var sink in _sinks) | ||
{ | ||
try | ||
{ | ||
sink.Emit(logEvent); | ||
} | ||
catch (Exception ex) | ||
{ | ||
SelfLog.WriteLine("Caught exception while emitting to sink {0}: {1}", sink, ex); | ||
exceptions = exceptions ?? new List<Exception>(); | ||
exceptions.Add(ex); | ||
} | ||
} | ||
|
||
if (exceptions != null) | ||
throw new AggregateException("Failed to emit a log event.", exceptions); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.