本文整理汇总了C#中LoggerConfiguration.Write方法的典型用法代码示例。如果您正苦于以下问题:C# LoggerConfiguration.Write方法的具体用法?C# LoggerConfiguration.Write怎么用?C# LoggerConfiguration.Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LoggerConfiguration
的用法示例。
在下文中一共展示了LoggerConfiguration.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestRollingEventSequence
static void TestRollingEventSequence(
IEnumerable<LogEvent> events,
int? retainedFiles,
Action<IList<string>> verifyWritten)
{
var fileName = Some.String() + "-{Date}.txt";
var folder = Some.TempFolderPath();
var pathFormat = Path.Combine(folder, fileName);
var log = new LoggerConfiguration()
.WriteTo.RollingFile(pathFormat, retainedFileCountLimit: retainedFiles)
.CreateLogger();
var verified = new List<string>();
try
{
foreach (var @event in events)
{
Clock.SetTestDateTimeNow(@event.Timestamp.DateTime);
log.Write(@event);
var expected = pathFormat.Replace("{Date}", @event.Timestamp.ToString("yyyyMMdd"));
Assert.That(File.Exists(expected));
verified.Add(expected);
}
}
finally
{
((IDisposable)log).Dispose();
verifyWritten(verified);
Directory.Delete(folder, true);
}
}
示例2: MoreNestedPropertiesOverrideLessNestedOnes
public void MoreNestedPropertiesOverrideLessNestedOnes()
{
LogEvent lastEvent = null;
var log = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Sink(new DelegatingSink(e => lastEvent = e))
.CreateLogger();
using (LogContext.PushProperty("A", 1))
{
log.Write(Some.InformationEvent());
Assert.AreEqual(1, lastEvent.Properties["A"].LiteralValue());
using (LogContext.PushProperty("A", 2))
{
log.Write(Some.InformationEvent());
Assert.AreEqual(2, lastEvent.Properties["A"].LiteralValue());
}
log.Write(Some.InformationEvent());
Assert.AreEqual(1, lastEvent.Properties["A"].LiteralValue());
}
log.Write(Some.InformationEvent());
Assert.IsFalse(lastEvent.Properties.ContainsKey("A"));
}
示例3: TestRollingEventSequence
static void TestRollingEventSequence(IEnumerable<LogEvent> events, int? retainedFiles, Action<IList<string>> verifyWritten)
{
var fileName = Some.String() + "-{Date}.txt";
var folder = Some.TempFolderPath();
var pathFormat = Path.Combine(folder, fileName);
var log = new LoggerConfiguration()
.WriteTo.SizeRollingFile(pathFormat, retainedFileDurationLimit: TimeSpan.FromSeconds(180))
.CreateLogger();
var verified = new List<string>();
try
{
foreach (var @event in events)
{
log.Write(@event);
var expected = pathFormat.Replace("{Date}", DateTime.UtcNow.ToString("yyyyMMdd") + "_0001");
Assert.True(File.Exists(expected));
verified.Add(expected);
}
}
finally
{
((IDisposable)log).Dispose();
verifyWritten(verified);
Directory.Delete(folder, true);
}
}
示例4: TestLoggingAndDelete
static void TestLoggingAndDelete(string path)
{
ILogger log = null;
try
{
log = new LoggerConfiguration()
.WriteTo.File(path)
.CreateLogger();
var message = Some.MessageTemplate();
log.Write(new LogEvent(
DateTimeOffset.Now,
LogEventLevel.Information,
null,
message,
Enumerable.Empty<LogEventProperty>()));
var refile = System.IO.File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var content = new StreamReader(refile).ReadToEnd();
refile.Dispose();
Assert.That(content.Contains(message.Text));
}
finally
{
var disposable = (IDisposable) log;
if (disposable != null) disposable.Dispose();
System.IO.File.Delete(path);
}
}
示例5: TestRollingEventSequence
static void TestRollingEventSequence(params LogEvent[] events)
{
var fileName = Some.String() + "{0}.txt";
var folder = Some.TempFolderPath();
var pathFormat = Path.Combine(folder, fileName);
var log = new LoggerConfiguration()
.WriteTo.RollingFile(pathFormat)
.CreateLogger();
try
{
foreach (var @event in events)
{
log.Write(@event);
var expected = string.Format(pathFormat, @event.Timestamp.ToString("yyyy-MM-dd"));
Assert.That(System.IO.File.Exists(expected));
}
}
finally
{
((IDisposable)log).Dispose();
Directory.Delete(folder, true);
}
}
示例6: ContextPropertiesCrossAsyncCalls
public async Task ContextPropertiesCrossAsyncCalls()
{
LogEvent lastEvent = null;
var log = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Sink(new DelegatingSink(e => lastEvent = e))
.CreateLogger();
using (LogContext.PushProperty("A", 1))
{
var pre = Thread.CurrentThread.ManagedThreadId;
await Task.Delay(1000);
var post = Thread.CurrentThread.ManagedThreadId;
log.Write(Some.InformationEvent());
Assert.AreEqual(1, lastEvent.Properties["A"].LiteralValue());
// No problem if this happens occasionally.
if (pre == post)
Assert.Inconclusive("The test was marshalled back to the same thread after awaiting");
}
}
示例7: AFilterPreventsMatchedEventsFromPassingToTheSink
public void AFilterPreventsMatchedEventsFromPassingToTheSink()
{
var excluded = Some.InformationEvent();
var included = Some.InformationEvent();
var filter = new DelegateFilter(e => e.MessageTemplate != excluded.MessageTemplate);
var events = new List<LogEvent>();
var sink = new DelegatingSink(events.Add);
var logger = new LoggerConfiguration()
.WriteTo.Sink(sink)
.Filter.With(filter)
.CreateLogger();
logger.Write(included);
logger.Write(excluded);
Assert.AreEqual(1, events.Count);
Assert.That(events.Contains(included));
}
示例8: SourceFiltersWorkOnNamespaces
public void SourceFiltersWorkOnNamespaces()
{
var written = false;
var log = new LoggerConfiguration()
.Filter.ByExcluding(Matching.FromSource("Serilog.Tests"))
.WriteTo.Sink(new DelegatingSink(e => written = true))
.CreateLogger()
.ForContext<MatchingTests>();
log.Write(Some.InformationEvent());
Assert.False(written);
}
示例9: EventsAreWrittenToObservers
public void EventsAreWrittenToObservers()
{
var eventSeen = false;
var log = new LoggerConfiguration()
.WriteTo.Observers(events => events
.Do(evt => { eventSeen = true; })
.Subscribe())
.CreateLogger();
log.Write(Some.InformationEvent());
Assert.That(eventSeen);
}
示例10: EnrichersExecuteInConfigurationOrder
public void EnrichersExecuteInConfigurationOrder()
{
var property = Some.LogEventProperty();
var enrichedPropertySeen = false;
var logger = new LoggerConfiguration()
.Enrich.With(new DelegatingEnricher((e, f) => e.AddPropertyIfAbsent(property)))
.Enrich.With(new DelegatingEnricher((e, f) => enrichedPropertySeen = e.Properties.ContainsKey(property.Name)))
.CreateLogger();
logger.Write(Some.InformationEvent());
Assert.That(enrichedPropertySeen);
}
示例11: ChildLoggerInheritsParentLevelByDefault
public void ChildLoggerInheritsParentLevelByDefault()
{
var sink = new CollectingSink();
var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Logger(lc => lc
.WriteTo.Sink(sink))
.CreateLogger();
logger.Write(Some.DebugEvent());
Assert.Equal(1, sink.Events.Count);
}
示例12: ChildLoggerCanOverrideInheritedLevel
public void ChildLoggerCanOverrideInheritedLevel()
{
var sink = new CollectingSink();
var logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(new LoggingLevelSwitch(LogEventLevel.Debug))
.WriteTo.Logger(lc => lc
.MinimumLevel.Error()
.WriteTo.Sink(sink))
.CreateLogger();
logger.Write(Some.DebugEvent());
Assert.Equal(0, sink.Events.Count);
}
示例13: AuditSinksAreConfigured
public void AuditSinksAreConfigured()
{
var settings = new Dictionary<string, string>
{
["using:TestDummies"] = typeof(DummyLoggerConfigurationExtensions).GetTypeInfo().Assembly.FullName,
["audit-to:DummyRollingFile.pathFormat"] = "C:\\"
};
var log = new LoggerConfiguration()
.ReadFrom.KeyValuePairs(settings)
.CreateLogger();
DummyRollingFileSink.Emitted.Clear();
DummyRollingFileAuditSink.Emitted.Clear();
log.Write(Some.InformationEvent());
Assert.Equal(0, DummyRollingFileSink.Emitted.Count);
Assert.Equal(1, DummyRollingFileAuditSink.Emitted.Count);
}
示例14: ExceptionsThrownByFiltersAreNotPropagated
public void ExceptionsThrownByFiltersAreNotPropagated()
{
var logger = new LoggerConfiguration()
.Filter.ByExcluding(e => { throw new Exception("Boom!"); })
.CreateLogger();
logger.Write(Some.InformationEvent());
Assert.True(true, "No exception reached the caller");
}
示例15: WhenSuspendedAllPropertiesAreRemovedFromTheContext
public void WhenSuspendedAllPropertiesAreRemovedFromTheContext()
{
LogEvent lastEvent = null;
var log = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Sink(new DelegatingSink(e => lastEvent = e))
.CreateLogger();
using (LogContext.PushProperty("A1", 1))
{
using (LogContext.Suspend())
{
log.Write(Some.InformationEvent());
Assert.IsFalse(lastEvent.Properties.ContainsKey("A1"));
}
log.Write(Some.InformationEvent());
Assert.AreEqual(1, lastEvent.Properties["A1"].LiteralValue());
}
}