Skip to content

Commit

Permalink
Switch to use NLog LogEntry to preserve callsite
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jul 10, 2015
1 parent 20f03b1 commit 180fc5d
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/ServiceStack.Interfaces/ServiceStack.Interfaces.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{55942102-033A-4DA8-A6AF-1DB7B2F34A2D}</ProjectGuid>
<ProjectGuid>{42E1C8C0-A163-44CC-92B1-8F416F2C0B01}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ServiceStack</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
Expand Down
44 changes: 27 additions & 17 deletions src/ServiceStack.Logging.NLog/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public NLogLogger(Type type)
public bool IsErrorEnabled { get { return log.IsErrorEnabled; } }

public bool IsFatalEnabled { get { return log.IsFatalEnabled; } }

private static string AsString(object message)
{
return message != null ? message.ToString() : null;
Expand All @@ -49,7 +49,7 @@ private static string AsString(object message)
public void Debug(object message)
{
if (IsDebugEnabled)
log.Debug(message);
Log(LogLevel.Debug, AsString(message));
}

/// <summary>
Expand All @@ -59,8 +59,8 @@ public void Debug(object message)
/// <param name="exception">The exception.</param>
public void Debug(object message, Exception exception)
{
if(IsDebugEnabled)
log.DebugException(AsString(message), exception);
if (IsDebugEnabled)
Log(LogLevel.Debug, AsString(message), exception);
}

/// <summary>
Expand All @@ -71,7 +71,7 @@ public void Debug(object message, Exception exception)
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
log.Debug(format, args);
Log(LogLevel.Debug, format, args);
}

/// <summary>
Expand All @@ -81,7 +81,7 @@ public void DebugFormat(string format, params object[] args)
public void Error(object message)
{
if (IsErrorEnabled)
log.Error(message);
Log(LogLevel.Error, AsString(message));
}

/// <summary>
Expand All @@ -92,7 +92,7 @@ public void Error(object message)
public void Error(object message, Exception exception)
{
if (IsErrorEnabled)
log.Error(AsString(message), exception);
Log(LogLevel.Error, AsString(message), exception);
}

/// <summary>
Expand All @@ -103,7 +103,7 @@ public void Error(object message, Exception exception)
public void ErrorFormat(string format, params object[] args)
{
if (IsErrorEnabled)
log.Error(format,args);
Log(LogLevel.Error, format, args);
}

/// <summary>
Expand All @@ -113,7 +113,7 @@ public void ErrorFormat(string format, params object[] args)
public void Fatal(object message)
{
if (IsFatalEnabled)
log.Fatal(message);
Log(LogLevel.Fatal, AsString(message));
}

/// <summary>
Expand All @@ -124,7 +124,7 @@ public void Fatal(object message)
public void Fatal(object message, Exception exception)
{
if (IsFatalEnabled)
log.FatalException(AsString(message), exception);
Log(LogLevel.Fatal, AsString(message), exception);
}

/// <summary>
Expand All @@ -135,7 +135,7 @@ public void Fatal(object message, Exception exception)
public void FatalFormat(string format, params object[] args)
{
if (IsFatalEnabled)
log.Fatal(format, args);
Log(LogLevel.Fatal, format, args);
}

/// <summary>
Expand All @@ -145,7 +145,7 @@ public void FatalFormat(string format, params object[] args)
public void Info(object message)
{
if (IsInfoEnabled)
log.Info(message);
Log(LogLevel.Info, AsString(message));
}

/// <summary>
Expand All @@ -156,7 +156,7 @@ public void Info(object message)
public void Info(object message, Exception exception)
{
if (IsInfoEnabled)
log.InfoException(AsString(message), exception);
Log(LogLevel.Info, AsString(message), exception);
}

/// <summary>
Expand All @@ -167,7 +167,7 @@ public void Info(object message, Exception exception)
public void InfoFormat(string format, params object[] args)
{
if (IsInfoEnabled)
log.Info(format, args);
Log(LogLevel.Info, format, args);
}

/// <summary>
Expand All @@ -177,7 +177,7 @@ public void InfoFormat(string format, params object[] args)
public void Warn(object message)
{
if (IsWarnEnabled)
log.Warn(message);
Log(LogLevel.Warn, AsString(message));
}

/// <summary>
Expand All @@ -188,7 +188,7 @@ public void Warn(object message)
public void Warn(object message, Exception exception)
{
if (IsWarnEnabled)
log.WarnException(AsString(message), exception);
Log(LogLevel.Warn, AsString(message), exception);
}

/// <summary>
Expand All @@ -199,7 +199,17 @@ public void Warn(object message, Exception exception)
public void WarnFormat(string format, params object[] args)
{
if (IsWarnEnabled)
log.Warn(format, args);
Log(LogLevel.Warn, format, args);
}

public void Log(NLog.LogLevel logLevel, string format, params object[] args)
{
log.Log(typeof(NLogLogger), new LogEventInfo(LogLevel.Info, log.Name, null, format, args));
}

public void Log(NLog.LogLevel logLevel, string format, object[] args, Exception ex)
{
log.Log(typeof(NLogLogger), new LogEventInfo(LogLevel.Info, log.Name, null, format, args, ex));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@
<Reference Include="Microsoft.Practices.Unity.Interception.Configuration, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\src\packages\Unity.Interception.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.Interception.Configuration.dll</HintPath>
</Reference>
<Reference Include="NLog">
<HintPath>..\..\src\packages\NLog.2.1.0\lib\net40\NLog.dll</HintPath>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\..\src\packages\NLog.4.0.1\lib\net40\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\..\src\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\..\src\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Rhino.Mocks, Version=3.6.0.0, Culture=neutral, PublicKeyToken=0b3305902db7183f, processorArchitecture=MSIL">
<HintPath>..\..\src\packages\RhinoMocks.3.6.1\lib\net\Rhino.Mocks.dll</HintPath>
Expand All @@ -120,6 +122,7 @@
<Compile Include="UnitTests\Log4NetFactoryTests.cs" />
<Compile Include="UnitTests\Log4NetLoggerTests.cs" />
<Compile Include="UnitTests\LogManagerTests.cs" />
<Compile Include="UnitTests\NLogTests.cs" />
<Compile Include="UnitTests\UnitTestBase.cs" />
<Compile Include="UseCases\UsingEntLib5.cs" />
<Compile Include="UseCases\UsingEventLog.cs" />
Expand Down
20 changes: 20 additions & 0 deletions tests/ServiceStack.Logging.Tests/UnitTests/NLogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using NUnit.Framework;

namespace ServiceStack.Logging.Tests.UnitTests
{
[TestFixture]
public class NLogTests
{
[Test]
public void Does_maintain_callsite()
{
ServiceStack.Logging.LogManager.LogFactory = new ServiceStack.Logging.NLogger.NLogFactory();

var log = ServiceStack.Logging.LogManager.LogFactory.GetLogger(GetType());
log.InfoFormat("Message");
log.InfoFormat("Message with Args {0}", "Foo");
log.Info("Message with Exception", new Exception("Foo Exception"));
}
}
}
104 changes: 57 additions & 47 deletions tests/ServiceStack.Logging.Tests/app.config
Original file line number Diff line number Diff line change
@@ -1,53 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<!--Flat File Trace Listener-->
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<!--Flat File Trace Listener-->

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Default Category" logWarningsWhenNoCategoriesMatch="true">
<formatters>
<add name="Text Formatter" template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="One Line Formatter" template="{timestamp(local)} Cat: {category} Pri: {priority} EId: {eventid} Sev: {severity} {message} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id: {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties: {dictionary({key} - {value})}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</formatters>
<listeners>
<!-- Trace Listener to log entry to the System Event log using the Text Formatter format from above -->
<add name="Formatted EventLog TraceListener" source="Enterprise Library Logging" formatter="Text Formatter" log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<!-- Trace Listener to log entry to a Rolling Text File log using the One Line Formatter format from above -->
<add name="Rolling Flat File" fileName="Logger.log" formatter="One Line Formatter" rollFileExistsBehavior="Increment" rollInterval="Midnight" rollSizeKB="10000" timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</listeners>
<logFilters>
<add name="LogEnabled Filter" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" enabled="true" />
</logFilters>
<categorySources>
<!-- Log all Warnings to the System Event Log -->
<add name="Default Category" switchValue="Warning">
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Default Category" logWarningsWhenNoCategoriesMatch="true">
<formatters>
<add name="Text Formatter" template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="One Line Formatter" template="{timestamp(local)} Cat: {category} Pri: {priority} EId: {eventid} Sev: {severity} {message} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id: {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties: {dictionary({key} - {value})}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</formatters>
<listeners>
<add name="Formatted EventLog TraceListener" />
<!-- Trace Listener to log entry to the System Event log using the Text Formatter format from above -->
<add name="Formatted EventLog TraceListener" source="Enterprise Library Logging" formatter="Text Formatter" log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<!-- Trace Listener to log entry to a Rolling Text File log using the One Line Formatter format from above -->
<add name="Rolling Flat File" fileName="Logger.log" formatter="One Line Formatter" rollFileExistsBehavior="Increment" rollInterval="Midnight" rollSizeKB="10000" timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</listeners>
</add>
</categorySources>
<specialSources>
<!-- Log everyting not specified above to the Rolling Text File log -->
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="Rolling Flat File" />
</listeners>
</allEvents>
<errors switchValue="All" name="Logging Errors &amp; Warnings">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<logFilters>
<add name="LogEnabled Filter" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" enabled="true" />
</logFilters>
<categorySources>
<!-- Log all Warnings to the System Event Log -->
<add name="Default Category" switchValue="Warning">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</add>
</categorySources>
<specialSources>
<!-- Log everyting not specified above to the Rolling Text File log -->
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="Rolling Flat File" />
</listeners>
</allEvents>
<errors switchValue="All" name="Logging Errors &amp; Warnings">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="ColoredConsole"
layout="${machinename} ${message} ${callsite}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="console"></logger>
</rules>
</nlog>
</configuration>
4 changes: 2 additions & 2 deletions tests/ServiceStack.Logging.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<package id="EnterpriseLibrary.Common" version="5.0.505.0" targetFramework="net35" />
<package id="EnterpriseLibrary.Logging" version="5.0.505.1" targetFramework="net35" />
<package id="log4net" version="2.0.3" targetFramework="net40" />
<package id="NLog" version="2.1.0" targetFramework="net40" />
<package id="NUnit" version="2.6.3" targetFramework="net40" />
<package id="NLog" version="4.0.1" targetFramework="net40" />
<package id="NUnit" version="2.6.4" targetFramework="net40" />
<package id="RhinoMocks" version="3.6.1" targetFramework="net35" />
<package id="Unity" version="2.1.505.2" targetFramework="net35" />
<package id="Unity.Interception" version="2.1.505.2" targetFramework="net35" />
Expand Down

0 comments on commit 180fc5d

Please sign in to comment.