Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Serilog logging. #1110

Merged
merged 8 commits into from Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NuGet/NuGetPack.cmd
Expand Up @@ -27,6 +27,7 @@ SET NUGET=..\src\.nuget\nuget
%NUGET% pack ServiceStack.Logging.EventLog\servicestack.logging.eventlog.nuspec -symbols
%NUGET% pack ServiceStack.Logging.Log4Net\servicestack.logging.log4net.nuspec -symbols
%NUGET% pack ServiceStack.Logging.NLog\servicestack.logging.nlog.nuspec -symbols
%NUGET% pack ServiceStack.Logging.Serilog\servicestack.logging.serilog.nuspec -symbols

%NUGET% pack ServiceStack.ProtoBuf\servicestack.protobuf.nuspec -symbols
%NUGET% pack ServiceStack.MsgPack\servicestack.msgpack.nuspec -symbols
Expand Down
13 changes: 13 additions & 0 deletions build/build.proj
Expand Up @@ -133,6 +133,7 @@
<EventLogFiles Include="$(SrcDir)/ServiceStack.Logging.EventLog/bin/$(Configuration)/ServiceStack.Logging.EventLog.*"></EventLogFiles>
<Log4NetFiles Include="$(SrcDir)/ServiceStack.Logging.Log4Net/bin/$(Configuration)/ServiceStack.Logging.Log4Net.*"></Log4NetFiles>
<NLogFiles Include="$(SrcDir)/ServiceStack.Logging.NLog/bin/$(Configuration)/ServiceStack.Logging.NLog.*"></NLogFiles>
<SerilogFiles Include="$(SrcDir)/ServiceStack.Logging.Serilog/bin/$(Configuration)/ServiceStack.Logging.Serilog.*"></SerilogFiles>
<SlackFiles Include="$(SrcDir)/ServiceStack.Logging.Slack/bin/$(Configuration)/ServiceStack.Logging.Slack.*"></SlackFiles>

<ProtoBufFiles Include="$(SrcDir)/ServiceStack.ProtoBuf/bin/$(Configuration)/ServiceStack.ProtoBuf.*"></ProtoBufFiles>
Expand Down Expand Up @@ -315,6 +316,18 @@
<Exec Command="&quot;$(NuGetPath)&quot; pack &quot;$(NuGetPackageDir)/ServiceStack.Logging.NLog/servicestack.logging.nlog.nuspec&quot; -OutputDirectory &quot;$(NuGetPackageDir)&quot; -Version $(PackageVersion) -Symbols"
LogStandardErrorAsError="true" />

<!-- ServiceStack.Logging.Serilog -->

<MSBuild Projects="$(SrcDir)/ServiceStack.Logging.Serilog/ServiceStack.Logging.Serilog.csproj"
Targets="Build"
Properties="Version=$(PackageVersion);Configuration=$(Configuration)" />

<MakeDir Directories="$(NuGetPackageDir)/ServiceStack.Logging.Serilog/lib/net45" Condition="!Exists('$(NuGetPackageDir)/ServiceStack.Logging.Serilog/lib/net45')" />
<Copy SourceFiles="%(SerilogFiles.Identity)" DestinationFolder="$(NuGetPackageDir)/ServiceStack.Logging.Serilog/lib/net45" />

<Exec Command="&quot;$(NuGetPath)&quot; pack &quot;$(NuGetPackageDir)/ServiceStack.Logging.Serilog/servicestack.logging.Serilog.nuspec&quot; -OutputDirectory &quot;$(NuGetPackageDir)&quot; -Version $(PackageVersion) -Symbols"
LogStandardErrorAsError="true" />

<!-- ServiceStack.Logging.Slack -->

<MSBuild Projects="$(SrcDir)/ServiceStack.Logging.Slack/ServiceStack.Logging.Slack.csproj"
Expand Down
41 changes: 41 additions & 0 deletions src/ServiceStack.Logging.Serilog/Properties/AssemblyInfo.cs
@@ -0,0 +1,41 @@
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("ServiceStack.Logging.Serilog")]
[assembly: AssemblyDescription(@"Provides serilog logging integration for other ServiceStack projects
Includes:
- ServiceStack.Logging.Serilog.dll
Dependencies:
- ServiceStack.Interfaces.dll")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Demis Bellot")]
[assembly: AssemblyProduct("ServiceStack.Logging.Serilog")]
[assembly: AssemblyCopyright("Copyright � ServiceStack 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]


// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d13c55f2-2688-41e1-b4d1-6b6609851844")]

// 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")]
30 changes: 30 additions & 0 deletions src/ServiceStack.Logging.Serilog/SerilogFactory.cs
@@ -0,0 +1,30 @@
using System;

namespace ServiceStack.Logging.Serilog
{
/// <summary>
/// Implementation of <see cref="ILogFactory"/> that creates a <see cref="Serilog"/> <see cref="ILog"/> Logger.
/// </summary>
public class SerilogFactory : ILogFactory
{
/// <summary>
/// Gets the logger.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public ILog GetLogger(Type type)
{
return new SerilogLogger(type);
}

/// <summary>
/// Gets the logger.
/// </summary>
/// <param name="typeName">Name of the type.</param>
/// <returns></returns>
public ILog GetLogger(string typeName)
{
return new SerilogLogger(Type.GetType(typeName));
}
}
}
244 changes: 244 additions & 0 deletions src/ServiceStack.Logging.Serilog/SerilogLogger.cs
@@ -0,0 +1,244 @@
using System;
using Serilog;
using Serilog.Events;

namespace ServiceStack.Logging.Serilog
{
/// <summary>
/// Implementation of <see cref="ILog"/> for <see cref="Serilog"/>.
/// </summary>
public class SerilogLogger : ILog
{
private readonly ILogger _log;

/// <summary>
/// Initializes a new instance of the <see cref="SerilogLogger"/> class.
/// </summary>
/// <param name="type">The <see cref="Type"/>.</param>
public SerilogLogger(Type type)
{
_log = Log.ForContext(type);
}

/// <summary>
/// Gets a value indicating if Debug messages are enabled.
/// </summary>
public bool IsDebugEnabled => _log.IsEnabled(LogEventLevel.Debug);

/// <summary>
/// Gets a value indicating if Info messages are enabled.
/// </summary>
public bool IsInfoEnabled => _log.IsEnabled(LogEventLevel.Information);

/// <summary>
/// Gets a value indicating if Warning messages are enabled.
/// </summary>
public bool IsWarnEnabled => _log.IsEnabled(LogEventLevel.Warning);

/// <summary>
/// Gets a value indicating if Error messages are enabled.
/// </summary>
public bool IsErrorEnabled => _log.IsEnabled(LogEventLevel.Error);

/// <summary>
/// Gets a value indicating if Fatal messages are enabled.
/// </summary>
public bool IsFatalEnabled => _log.IsEnabled(LogEventLevel.Fatal);

/// <summary>
/// Logs a Debug message.
/// </summary>
/// <param name="message">The message.</param>
public void Debug(object message)
{
if (IsDebugEnabled)
Write(LogEventLevel.Debug, message);
}

/// <summary>
/// Logs a Debug message and exception.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="exception">The exception.</param>
public void Debug(object message, Exception exception)
{
if (IsDebugEnabled)
Write(LogEventLevel.Debug, message, exception);
}

/// <summary>
/// Logs a Debug format message.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
Write(LogEventLevel.Debug, format, args);
}

/// <summary>
/// Logs a Error message.
/// </summary>
/// <param name="message">The message.</param>
public void Error(object message)
{
if (IsErrorEnabled)
Write(LogEventLevel.Error, message);
}

/// <summary>
/// Logs a Error message and exception.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="exception">The exception.</param>
public void Error(object message, Exception exception)
{
if (IsErrorEnabled)
Write(LogEventLevel.Error, message, exception);
}

/// <summary>
/// Logs a Error format message.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
public void ErrorFormat(string format, params object[] args)
{
if (IsErrorEnabled)
Write(LogEventLevel.Error, format, args);
}

/// <summary>
/// Logs a Fatal message.
/// </summary>
/// <param name="message">The message.</param>
public void Fatal(object message)
{
if (IsFatalEnabled)
Write(LogEventLevel.Fatal, message);
}

/// <summary>
/// Logs a Fatal message and exception.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="exception">The exception.</param>
public void Fatal(object message, Exception exception)
{
if (IsFatalEnabled)
Write(LogEventLevel.Fatal, message, exception);
}

/// <summary>
/// Logs a Fatal format message.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
public void FatalFormat(string format, params object[] args)
{
if (IsFatalEnabled)
Write(LogEventLevel.Fatal, format, args);
}

/// <summary>
/// Logs a Info message.
/// </summary>
/// <param name="message">The message.</param>
public void Info(object message)
{
if (IsInfoEnabled)
Write(LogEventLevel.Information, message);
}

/// <summary>
/// Logs a Info message and exception.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="exception">The exception.</param>
public void Info(object message, Exception exception)
{
if (IsInfoEnabled)
Write(LogEventLevel.Information, message, exception);
}

/// <summary>
/// Logs a Info format message.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
public void InfoFormat(string format, params object[] args)
{
if (IsInfoEnabled)
Write(LogEventLevel.Information, format, args);
}

/// <summary>
/// Logs a Warning message.
/// </summary>
/// <param name="message">The message.</param>
public void Warn(object message)
{
if (IsWarnEnabled)
Write(LogEventLevel.Warning, message);
}

/// <summary>
/// Logs a Warning message and exception.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="exception">The exception.</param>
public void Warn(object message, Exception exception)
{
if (IsWarnEnabled)
Write(LogEventLevel.Warning, message, exception);
}

/// <summary>
/// Logs a Warning format message.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
public void WarnFormat(string format, params object[] args)
{
if (IsWarnEnabled)
Write(LogEventLevel.Warning, format, args);
}

private void Write(LogEventLevel level, object message)
{
var messageTemplate = message as string;
if (messageTemplate != null)
{
_log.Write(level, messageTemplate);
return;
}

var exception = message as Exception;
if (exception != null)
{
_log.Write(level, exception, exception.GetType().Name);
return;
}

_log.Write(level, message.ToString());
}

private void Write(LogEventLevel level, object message, Exception exception)
{
var messageTemplate = message as string;
if (messageTemplate != null)
{
_log.Write(level, exception, messageTemplate);
return;
}

_log.Write(level, exception, message.ToString());
}

private void Write(LogEventLevel level, string format, params object[] args)
{
_log.Write(level, format, args);
}
}
}