46 changed files with 269 additions and 405 deletions
@ -1,7 +0,0 @@ |
|||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
||||
<ItemGroup> |
|
||||
<PackageReference Include="Serilog" Version="2.5.0" /> |
|
||||
<PackageReference Include="Serilog.Sinks.Trace" Version="2.1.0" /> |
|
||||
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.0" /> |
|
||||
</ItemGroup> |
|
||||
</Project> |
|
||||
@ -0,0 +1,179 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Diagnostics; |
||||
|
using System.Text; |
||||
|
using Avalonia.Utilities; |
||||
|
|
||||
|
namespace Avalonia.Logging |
||||
|
{ |
||||
|
public class DebugLogSink : ILogSink |
||||
|
{ |
||||
|
private readonly LogEventLevel _level; |
||||
|
private readonly IList<string> _areas; |
||||
|
|
||||
|
public DebugLogSink( |
||||
|
LogEventLevel minimumLevel, |
||||
|
IList<string> areas = null) |
||||
|
{ |
||||
|
_level = minimumLevel; |
||||
|
_areas = areas?.Count > 0 ? areas : null; |
||||
|
} |
||||
|
|
||||
|
public bool IsEnabled(LogEventLevel level, string area) |
||||
|
{ |
||||
|
return level >= _level && (_areas?.Contains(area) ?? true); |
||||
|
} |
||||
|
|
||||
|
public void Log(LogEventLevel level, string area, object source, string messageTemplate) |
||||
|
{ |
||||
|
if (IsEnabled(level, area)) |
||||
|
{ |
||||
|
Debug.WriteLine(Format<object, object, object>(area, messageTemplate, source)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Log<T0>(LogEventLevel level, string area, object source, string messageTemplate, T0 propertyValue0) |
||||
|
{ |
||||
|
if (IsEnabled(level, area)) |
||||
|
{ |
||||
|
Debug.WriteLine(Format<T0, object, object>(area, messageTemplate, source, propertyValue0)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Log<T0, T1>(LogEventLevel level, string area, object source, string messageTemplate, T0 propertyValue0, T1 propertyValue1) |
||||
|
{ |
||||
|
if (IsEnabled(level, area)) |
||||
|
{ |
||||
|
Debug.WriteLine(Format<T0, T1, object>(area, messageTemplate, source, propertyValue0, propertyValue1)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Log<T0, T1, T2>(LogEventLevel level, string area, object source, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) |
||||
|
{ |
||||
|
if (IsEnabled(level, area)) |
||||
|
{ |
||||
|
Debug.WriteLine(Format(area, messageTemplate, source, propertyValue0, propertyValue1, propertyValue2)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Log(LogEventLevel level, string area, object source, string messageTemplate, params object[] propertyValues) |
||||
|
{ |
||||
|
if (IsEnabled(level, area)) |
||||
|
{ |
||||
|
Debug.WriteLine(Format(area, messageTemplate, source, propertyValues)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static string Format<T0, T1, T2>( |
||||
|
string area, |
||||
|
string template, |
||||
|
object source, |
||||
|
T0 v0 = default, |
||||
|
T1 v1 = default, |
||||
|
T2 v2 = default) |
||||
|
{ |
||||
|
var result = new StringBuilder(template.Length); |
||||
|
var r = new CharacterReader(template.AsSpan()); |
||||
|
var i = 0; |
||||
|
|
||||
|
result.Append('['); |
||||
|
result.Append(area); |
||||
|
result.Append("] "); |
||||
|
|
||||
|
while (!r.End) |
||||
|
{ |
||||
|
var c = r.Take(); |
||||
|
|
||||
|
if (c != '{') |
||||
|
{ |
||||
|
result.Append(c); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
if (r.Peek != '{') |
||||
|
{ |
||||
|
result.Append('\''); |
||||
|
result.Append(i++ switch |
||||
|
{ |
||||
|
0 => v0, |
||||
|
1 => v1, |
||||
|
2 => v2, |
||||
|
_ => null |
||||
|
}); |
||||
|
result.Append('\''); |
||||
|
r.TakeUntil('}'); |
||||
|
r.Take(); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
result.Append('{'); |
||||
|
r.Take(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (source is object) |
||||
|
{ |
||||
|
result.Append(" ("); |
||||
|
result.Append(source.GetType().Name); |
||||
|
result.Append(" #"); |
||||
|
result.Append(source.GetHashCode()); |
||||
|
result.Append(')'); |
||||
|
} |
||||
|
|
||||
|
return result.ToString(); |
||||
|
} |
||||
|
|
||||
|
private static string Format( |
||||
|
string area, |
||||
|
string template, |
||||
|
object source, |
||||
|
object[] v) |
||||
|
{ |
||||
|
var result = new StringBuilder(template.Length); |
||||
|
var r = new CharacterReader(template.AsSpan()); |
||||
|
var i = 0; |
||||
|
|
||||
|
result.Append('['); |
||||
|
result.Append(area); |
||||
|
result.Append(']'); |
||||
|
|
||||
|
while (!r.End) |
||||
|
{ |
||||
|
var c = r.Take(); |
||||
|
|
||||
|
if (c != '{') |
||||
|
{ |
||||
|
result.Append(c); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
if (r.Peek != '{') |
||||
|
{ |
||||
|
result.Append('\''); |
||||
|
result.Append(i < v.Length ? v[i++] : null); |
||||
|
result.Append('\''); |
||||
|
r.TakeUntil('}'); |
||||
|
r.Take(); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
result.Append('{'); |
||||
|
r.Take(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (source is object) |
||||
|
{ |
||||
|
result.Append('('); |
||||
|
result.Append(source.GetType().Name); |
||||
|
result.Append(" #"); |
||||
|
result.Append(source.GetHashCode()); |
||||
|
result.Append(')'); |
||||
|
} |
||||
|
|
||||
|
return result.ToString(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Logging; |
||||
|
|
||||
|
namespace Avalonia |
||||
|
{ |
||||
|
public static class LoggingExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Logs Avalonia events to the <see cref="System.Diagnostics.Debug"/> sink.
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="T">The application class type.</typeparam>
|
||||
|
/// <param name="builder">The app builder instance.</param>
|
||||
|
/// <param name="level">The minimum level to log.</param>
|
||||
|
/// <param name="areas">The areas to log. Valid values are listed in <see cref="LogArea"/>.</param>
|
||||
|
/// <returns>The app builder instance.</returns>
|
||||
|
public static T LogToDebug<T>( |
||||
|
this T builder, |
||||
|
LogEventLevel level = LogEventLevel.Warning, |
||||
|
params string[] areas) |
||||
|
where T : AppBuilderBase<T>, new() |
||||
|
{ |
||||
|
Logger.Sink = new DebugLogSink(level, areas); |
||||
|
return builder; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,10 +0,0 @@ |
|||||
<Project Sdk="Microsoft.NET.Sdk"> |
|
||||
<PropertyGroup> |
|
||||
<TargetFramework>netstandard2.0</TargetFramework> |
|
||||
</PropertyGroup> |
|
||||
<ItemGroup> |
|
||||
<ProjectReference Include="..\Avalonia.Base\Avalonia.Base.csproj" /> |
|
||||
<ProjectReference Include="..\Avalonia.Controls\Avalonia.Controls.csproj" /> |
|
||||
</ItemGroup> |
|
||||
<Import Project="..\..\build\Serilog.props" /> |
|
||||
</Project> |
|
||||
@ -1,103 +0,0 @@ |
|||||
using System; |
|
||||
using Avalonia.Controls; |
|
||||
using Serilog; |
|
||||
using Serilog.Configuration; |
|
||||
using Serilog.Filters; |
|
||||
using SerilogLevel = Serilog.Events.LogEventLevel; |
|
||||
|
|
||||
namespace Avalonia.Logging.Serilog |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Extension methods for Serilog logging.
|
|
||||
/// </summary>
|
|
||||
public static class SerilogExtensions |
|
||||
{ |
|
||||
private const string DefaultTemplate = "[{Area}] {Message} ({SourceType} #{SourceHash})"; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Logs Avalonia events to the <see cref="System.Diagnostics.Debug"/> sink.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="T">The application class type.</typeparam>
|
|
||||
/// <param name="builder">The app builder instance.</param>
|
|
||||
/// <param name="level">The minimum level to log.</param>
|
|
||||
/// <returns>The app builder instance.</returns>
|
|
||||
public static T LogToDebug<T>( |
|
||||
this T builder, |
|
||||
LogEventLevel level = LogEventLevel.Warning) |
|
||||
where T : AppBuilderBase<T>, new() |
|
||||
{ |
|
||||
SerilogLogger.Initialize(new LoggerConfiguration() |
|
||||
.MinimumLevel.Is((SerilogLevel)level) |
|
||||
.Enrich.FromLogContext() |
|
||||
.WriteTo.Debug(outputTemplate: DefaultTemplate) |
|
||||
.CreateLogger()); |
|
||||
return builder; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Logs Avalonia events to the <see cref="System.Diagnostics.Debug"/> sink.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="T">The application class type.</typeparam>
|
|
||||
/// <param name="builder">The app builder instance.</param>
|
|
||||
/// <param name="area">The area to log. Valid values are listed in <see cref="LogArea"/>.</param>
|
|
||||
/// <param name="level">The minimum level to log.</param>
|
|
||||
/// <returns>The app builder instance.</returns>
|
|
||||
public static T LogToDebug<T>( |
|
||||
this T builder, |
|
||||
string area, |
|
||||
LogEventLevel level = LogEventLevel.Warning) |
|
||||
where T : AppBuilderBase<T>, new() |
|
||||
{ |
|
||||
SerilogLogger.Initialize(new LoggerConfiguration() |
|
||||
.MinimumLevel.Is((SerilogLevel)level) |
|
||||
.Filter.ByIncludingOnly(Matching.WithProperty("Area", area)) |
|
||||
.Enrich.FromLogContext() |
|
||||
.WriteTo.Debug(outputTemplate: DefaultTemplate) |
|
||||
.CreateLogger()); |
|
||||
return builder; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Logs Avalonia events to the <see cref="System.Diagnostics.Trace"/> sink.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="T">The application class type.</typeparam>
|
|
||||
/// <param name="builder">The app builder instance.</param>
|
|
||||
/// <param name="level">The minimum level to log.</param>
|
|
||||
/// <returns>The app builder instance.</returns>
|
|
||||
public static T LogToTrace<T>( |
|
||||
this T builder, |
|
||||
LogEventLevel level = LogEventLevel.Warning) |
|
||||
where T : AppBuilderBase<T>, new() |
|
||||
{ |
|
||||
SerilogLogger.Initialize(new LoggerConfiguration() |
|
||||
.MinimumLevel.Is((SerilogLevel)level) |
|
||||
.Enrich.FromLogContext() |
|
||||
.WriteTo.Trace(outputTemplate: DefaultTemplate) |
|
||||
.CreateLogger()); |
|
||||
return builder; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Logs Avalonia events to the <see cref="System.Diagnostics.Trace"/> sink.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="T">The application class type.</typeparam>
|
|
||||
/// <param name="builder">The app builder instance.</param>
|
|
||||
/// <param name="area">The area to log. Valid values are listed in <see cref="LogArea"/>.</param>
|
|
||||
/// <param name="level">The minimum level to log.</param>
|
|
||||
/// <returns>The app builder instance.</returns>
|
|
||||
public static T LogToTrace<T>( |
|
||||
this T builder, |
|
||||
string area, |
|
||||
LogEventLevel level = LogEventLevel.Warning) |
|
||||
where T : AppBuilderBase<T>, new() |
|
||||
{ |
|
||||
SerilogLogger.Initialize(new LoggerConfiguration() |
|
||||
.MinimumLevel.Is((SerilogLevel)level) |
|
||||
.Filter.ByIncludingOnly(Matching.WithProperty("Area", area)) |
|
||||
.Enrich.FromLogContext() |
|
||||
.WriteTo.Trace(outputTemplate: DefaultTemplate) |
|
||||
.CreateLogger()); |
|
||||
return builder; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,151 +0,0 @@ |
|||||
using System; |
|
||||
using Serilog; |
|
||||
using Serilog.Context; |
|
||||
using AvaloniaLogEventLevel = Avalonia.Logging.LogEventLevel; |
|
||||
using SerilogLogEventLevel = Serilog.Events.LogEventLevel; |
|
||||
|
|
||||
namespace Avalonia.Logging.Serilog |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Sends log output to serilog.
|
|
||||
/// </summary>
|
|
||||
public class SerilogLogger : ILogSink |
|
||||
{ |
|
||||
private readonly ILogger _output; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="SerilogLogger"/> class.
|
|
||||
/// </summary>
|
|
||||
/// <param name="output">The serilog logger to use.</param>
|
|
||||
public SerilogLogger(ILogger output) |
|
||||
{ |
|
||||
_output = output; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Initializes the Avalonia logging with a new instance of a <see cref="SerilogLogger"/>.
|
|
||||
/// </summary>
|
|
||||
/// <param name="output">The serilog logger to use.</param>
|
|
||||
public static void Initialize(ILogger output) |
|
||||
{ |
|
||||
Logger.Sink = new SerilogLogger(output); |
|
||||
} |
|
||||
|
|
||||
public bool IsEnabled(LogEventLevel level) |
|
||||
{ |
|
||||
return _output.IsEnabled((SerilogLogEventLevel)level); |
|
||||
} |
|
||||
|
|
||||
public void Log( |
|
||||
LogEventLevel level, |
|
||||
string area, |
|
||||
object source, |
|
||||
string messageTemplate) |
|
||||
{ |
|
||||
Contract.Requires<ArgumentNullException>(area != null); |
|
||||
Contract.Requires<ArgumentNullException>(messageTemplate != null); |
|
||||
|
|
||||
using (PushLogContextProperties(area, source)) |
|
||||
{ |
|
||||
_output.Write((SerilogLogEventLevel)level, messageTemplate); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public void Log<T0>( |
|
||||
LogEventLevel level, |
|
||||
string area, object source, |
|
||||
string messageTemplate, |
|
||||
T0 propertyValue0) |
|
||||
{ |
|
||||
Contract.Requires<ArgumentNullException>(area != null); |
|
||||
Contract.Requires<ArgumentNullException>(messageTemplate != null); |
|
||||
|
|
||||
using (PushLogContextProperties(area, source)) |
|
||||
{ |
|
||||
_output.Write((SerilogLogEventLevel)level, messageTemplate, propertyValue0); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public void Log<T0, T1>( |
|
||||
LogEventLevel level, |
|
||||
string area, |
|
||||
object source, |
|
||||
string messageTemplate, |
|
||||
T0 propertyValue0, |
|
||||
T1 propertyValue1) |
|
||||
{ |
|
||||
Contract.Requires<ArgumentNullException>(area != null); |
|
||||
Contract.Requires<ArgumentNullException>(messageTemplate != null); |
|
||||
|
|
||||
using (PushLogContextProperties(area, source)) |
|
||||
{ |
|
||||
_output.Write((SerilogLogEventLevel)level, messageTemplate, propertyValue0, propertyValue1); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public void Log<T0, T1, T2>( |
|
||||
LogEventLevel level, |
|
||||
string area, |
|
||||
object source, |
|
||||
string messageTemplate, |
|
||||
T0 propertyValue0, |
|
||||
T1 propertyValue1, |
|
||||
T2 propertyValue2) |
|
||||
{ |
|
||||
Contract.Requires<ArgumentNullException>(area != null); |
|
||||
Contract.Requires<ArgumentNullException>(messageTemplate != null); |
|
||||
|
|
||||
using (PushLogContextProperties(area, source)) |
|
||||
{ |
|
||||
_output.Write((SerilogLogEventLevel)level, messageTemplate, propertyValue0, propertyValue1, propertyValue2); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public void Log( |
|
||||
AvaloniaLogEventLevel level, |
|
||||
string area, |
|
||||
object source, |
|
||||
string messageTemplate, |
|
||||
params object[] propertyValues) |
|
||||
{ |
|
||||
Contract.Requires<ArgumentNullException>(area != null); |
|
||||
Contract.Requires<ArgumentNullException>(messageTemplate != null); |
|
||||
|
|
||||
using (PushLogContextProperties(area, source)) |
|
||||
{ |
|
||||
_output.Write((SerilogLogEventLevel)level, messageTemplate, propertyValues); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private static LogContextDisposable PushLogContextProperties(string area, object source) |
|
||||
{ |
|
||||
return new LogContextDisposable( |
|
||||
LogContext.PushProperty("Area", area), |
|
||||
LogContext.PushProperty("SourceType", source?.GetType()), |
|
||||
LogContext.PushProperty("SourceHash", source?.GetHashCode()) |
|
||||
); |
|
||||
} |
|
||||
|
|
||||
private readonly struct LogContextDisposable : IDisposable |
|
||||
{ |
|
||||
private readonly IDisposable _areaDisposable; |
|
||||
private readonly IDisposable _sourceTypeDisposable; |
|
||||
private readonly IDisposable _sourceHashDisposable; |
|
||||
|
|
||||
public LogContextDisposable(IDisposable areaDisposable, IDisposable sourceTypeDisposable, IDisposable sourceHashDisposable) |
|
||||
{ |
|
||||
_areaDisposable = areaDisposable; |
|
||||
_sourceTypeDisposable = sourceTypeDisposable; |
|
||||
_sourceHashDisposable = sourceHashDisposable; |
|
||||
} |
|
||||
|
|
||||
public void Dispose() |
|
||||
{ |
|
||||
_areaDisposable.Dispose(); |
|
||||
_sourceTypeDisposable.Dispose(); |
|
||||
_sourceHashDisposable.Dispose(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue