Browse Source
Instead log through a static Logger, and provide a Serilog consumer for this.pull/486/head
35 changed files with 457 additions and 193 deletions
@ -0,0 +1,15 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
namespace Perspex.Logging |
|||
{ |
|||
public interface ILogSink |
|||
{ |
|||
void Log( |
|||
LogEventLevel level, |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues); |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
namespace Perspex.Logging |
|||
{ |
|||
/// <summary>
|
|||
/// Specifies the area in which a log event occurred.
|
|||
/// </summary>
|
|||
public static class LogArea |
|||
{ |
|||
/// <summary>
|
|||
/// The log event comes from the property and binding system.
|
|||
/// </summary>
|
|||
public const string Property = "Property"; |
|||
|
|||
/// <summary>
|
|||
/// The log event comes from the visual system.
|
|||
/// </summary>
|
|||
public const string Visual = "Visual"; |
|||
|
|||
/// <summary>
|
|||
/// The log event comes from the layout system.
|
|||
/// </summary>
|
|||
public const string Layout = "Layout"; |
|||
|
|||
/// <summary>
|
|||
/// The log event comes from the control system.
|
|||
/// </summary>
|
|||
public const string Control = "Control"; |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
namespace Perspex.Logging |
|||
{ |
|||
/// <summary>
|
|||
/// Specifies the meaning and relative importance of a log event.
|
|||
/// </summary>
|
|||
public enum LogEventLevel |
|||
{ |
|||
/// <summary>
|
|||
/// Anything and everything you might want to know about a running block of code.
|
|||
/// </summary>
|
|||
Verbose, |
|||
|
|||
/// <summary>
|
|||
/// Internal system events that aren't necessarily observable from the outside.
|
|||
/// </summary>
|
|||
Debug, |
|||
|
|||
/// <summary>
|
|||
/// The lifeblood of operational intelligence - things happen.
|
|||
/// </summary>
|
|||
Information, |
|||
|
|||
/// <summary>
|
|||
/// Service is degraded or endangered.
|
|||
/// </summary>
|
|||
Warning, |
|||
|
|||
/// <summary>
|
|||
/// Functionality is unavailable, invariants are broken or data is lost.
|
|||
/// </summary>
|
|||
Error, |
|||
|
|||
/// <summary>
|
|||
/// If you have a pager, it goes off when one of these occurs.
|
|||
/// </summary>
|
|||
Fatal |
|||
} |
|||
} |
|||
@ -0,0 +1,84 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
namespace Perspex.Logging |
|||
{ |
|||
public static class Logger |
|||
{ |
|||
public static ILogSink Sink { get; set; } |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static void Log( |
|||
LogEventLevel level, |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues) |
|||
{ |
|||
Sink?.Log(level, area, source, messageTemplate, propertyValues); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static void Verbose( |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues) |
|||
{ |
|||
Log(LogEventLevel.Verbose, area, source, messageTemplate, propertyValues); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static void Debug( |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues) |
|||
{ |
|||
Log(LogEventLevel.Debug, area, source, messageTemplate, propertyValues); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static void Information( |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues) |
|||
{ |
|||
Log(LogEventLevel.Information, area, source, messageTemplate, propertyValues); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static void Warning( |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues) |
|||
{ |
|||
Log(LogEventLevel.Warning, area, source, messageTemplate, propertyValues); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static void Error( |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues) |
|||
{ |
|||
Log(LogEventLevel.Error, area, source, messageTemplate, propertyValues); |
|||
} |
|||
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static void Fatal( |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues) |
|||
{ |
|||
Log(LogEventLevel.Fatal, area, source, messageTemplate, propertyValues); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> |
|||
<PropertyGroup> |
|||
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion> |
|||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|||
<ProjectGuid>{B61B66A3-B82D-4875-8001-89D3394FE0C9}</ProjectGuid> |
|||
<OutputType>Library</OutputType> |
|||
<AppDesignerFolder>Properties</AppDesignerFolder> |
|||
<RootNamespace>Perspex.Logging.Serilog</RootNamespace> |
|||
<AssemblyName>Perspex.Logging.Serilog</AssemblyName> |
|||
<DefaultLanguage>en-US</DefaultLanguage> |
|||
<FileAlignment>512</FileAlignment> |
|||
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> |
|||
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile> |
|||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|||
<DebugSymbols>true</DebugSymbols> |
|||
<DebugType>full</DebugType> |
|||
<Optimize>false</Optimize> |
|||
<OutputPath>bin\Debug\</OutputPath> |
|||
<DefineConstants>DEBUG;TRACE</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|||
<DebugType>pdbonly</DebugType> |
|||
<Optimize>true</Optimize> |
|||
<OutputPath>bin\Release\</OutputPath> |
|||
<DefineConstants>TRACE</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
</PropertyGroup> |
|||
<ItemGroup> |
|||
<Compile Include="SerilogLogger.cs" /> |
|||
<Compile Include="Properties\AssemblyInfo.cs" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<Reference Include="Serilog, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL"> |
|||
<HintPath>..\..\packages\Serilog.1.5.14\lib\portable-net45+win+wpa81+wp80+MonoAndroid10+MonoTouch10\Serilog.dll</HintPath> |
|||
<Private>True</Private> |
|||
</Reference> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<None Include="packages.config" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Perspex.Base\Perspex.Base.csproj"> |
|||
<Project>{b09b78d8-9b26-48b0-9149-d64a2f120f3f}</Project> |
|||
<Name>Perspex.Base</Name> |
|||
</ProjectReference> |
|||
</ItemGroup> |
|||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" /> |
|||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. |
|||
Other similar extension points exist, see Microsoft.Common.targets. |
|||
<Target Name="BeforeBuild"> |
|||
</Target> |
|||
<Target Name="AfterBuild"> |
|||
</Target> |
|||
--> |
|||
</Project> |
|||
@ -0,0 +1,30 @@ |
|||
using System.Resources; |
|||
using System.Reflection; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.InteropServices; |
|||
|
|||
// General Information about an assembly is controlled through the following
|
|||
// set of attributes. Change these attribute values to modify the information
|
|||
// associated with an assembly.
|
|||
[assembly: AssemblyTitle("Perspex.Serilog")] |
|||
[assembly: AssemblyDescription("")] |
|||
[assembly: AssemblyConfiguration("")] |
|||
[assembly: AssemblyCompany("")] |
|||
[assembly: AssemblyProduct("Perspex.Serilog")] |
|||
[assembly: AssemblyCopyright("Copyright © 2016")] |
|||
[assembly: AssemblyTrademark("")] |
|||
[assembly: AssemblyCulture("")] |
|||
[assembly: NeutralResourcesLanguage("en")] |
|||
|
|||
// Version information for an assembly consists of the following four values:
|
|||
//
|
|||
// Major Version
|
|||
// Minor Version
|
|||
// Build Number
|
|||
// Revision
|
|||
//
|
|||
// You can specify all the values or you can default the Build and Revision Numbers
|
|||
// by using the '*' as shown below:
|
|||
// [assembly: AssemblyVersion("1.0.*")]
|
|||
[assembly: AssemblyVersion("1.0.0.0")] |
|||
[assembly: AssemblyFileVersion("1.0.0.0")] |
|||
@ -0,0 +1,44 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Collections.Generic; |
|||
using Serilog; |
|||
using PerspexLogEventLevel = Perspex.Logging.LogEventLevel; |
|||
using SerilogLogEventLevel = Serilog.Events.LogEventLevel; |
|||
|
|||
namespace Perspex.Logging.Serilog |
|||
{ |
|||
public class SerilogLogger : ILogSink |
|||
{ |
|||
private readonly ILogger _output; |
|||
private readonly Dictionary<string, ILogger> _areas = new Dictionary<string, ILogger>(); |
|||
|
|||
public SerilogLogger(ILogger output) |
|||
{ |
|||
_output = output; |
|||
} |
|||
|
|||
public static void Initialize(ILogger output) |
|||
{ |
|||
Logger.Sink = new SerilogLogger(output); |
|||
} |
|||
|
|||
public void Log( |
|||
PerspexLogEventLevel level, |
|||
string area, |
|||
object source, |
|||
string messageTemplate, |
|||
params object[] propertyValues) |
|||
{ |
|||
ILogger areaLogger; |
|||
|
|||
if (!_areas.TryGetValue(area, out areaLogger)) |
|||
{ |
|||
areaLogger = _output.ForContext("Area", area); |
|||
_areas.Add(area, areaLogger); |
|||
} |
|||
|
|||
areaLogger.Write((SerilogLogEventLevel)level, messageTemplate, propertyValues); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,4 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<packages> |
|||
<package id="Serilog" version="1.5.14" targetFramework="portable45-net45+win8" /> |
|||
</packages> |
|||
Loading…
Reference in new issue