Skip to content

Commit 5aad314

Browse files
committed
Logging messages with data
1 parent 6127d9f commit 5aad314

File tree

15 files changed

+297
-16
lines changed

15 files changed

+297
-16
lines changed

DataSample/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

DataSample/DataSample.csproj

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{7CBF04DF-3FAB-4B4E-A4E0-5E6E3EA8EC8A}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>DataSample</RootNamespace>
11+
<AssemblyName>DataSample</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="Program.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<None Include="App.config" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<ProjectReference Include="..\Eleven41.Logging\Eleven41.Logging.csproj">
52+
<Project>{424b02c3-4fdb-4a6f-8263-89fb8df6c0e6}</Project>
53+
<Name>Eleven41.Logging</Name>
54+
</ProjectReference>
55+
</ItemGroup>
56+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
57+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
58+
Other similar extension points exist, see Microsoft.Common.targets.
59+
<Target Name="BeforeBuild">
60+
</Target>
61+
<Target Name="AfterBuild">
62+
</Target>
63+
-->
64+
</Project>

DataSample/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Eleven41.Logging;
7+
8+
namespace DataSample
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
ILog consoleLog = new DataConsoleLog();
15+
ILog dataLog = new DataLog(consoleLog, new Dictionary<string, object>()
16+
{
17+
{ "param1", "value1" },
18+
{ "param2", "value2" }
19+
});
20+
MyProcess(dataLog);
21+
22+
Console.WriteLine("Press Enter to continue...");
23+
Console.ReadLine();
24+
}
25+
26+
static void MyProcess(ILog log)
27+
{
28+
log.Log(LogLevels.Diagnostic, "This is a diagnostic message");
29+
log.Log(LogLevels.Info, "This is an information message");
30+
log.Log(LogLevels.Warning, new Dictionary<string, object>()
31+
{
32+
{ "param2", "override2" }
33+
}, "This is a warning message");
34+
log.Log(LogLevels.Error, "This is an error message: {0}", 1234);
35+
}
36+
}
37+
38+
// Create a custom version of the ConsoleLog to demonstrate sending
39+
// data with the message.
40+
// This implementation is for illustration purposes and should not be used in production.
41+
class DataConsoleLog : ConsoleLog
42+
{
43+
public override void Log(LogLevels level, Dictionary<string, object> data, string sFormat, params object[] args)
44+
{
45+
if (data != null)
46+
{
47+
List<string> keyValues = new List<string>();
48+
foreach (var kvp in data)
49+
keyValues.Add(String.Format("{0}={1}", kvp.Key, kvp.Value));
50+
51+
sFormat = sFormat + " (" + String.Join(",", keyValues) + ")";
52+
}
53+
54+
Log(level, sFormat, args);
55+
}
56+
}
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("DataSample")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Eleven41 Software Inc.")]
12+
[assembly: AssemblyProduct("DataSample")]
13+
[assembly: AssemblyCopyright("Copyright © 2014, Eleven41 Software Inc.")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("d8d3f127-ebec-4c26-9baa-dff95d790a54")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Eleven41.Logging.sln

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2012
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.30501.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
46
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Eleven41.Logging", "Eleven41.Logging\Eleven41.Logging.csproj", "{424B02C3-4FDB-4A6F-8263-89FB8DF6C0E6}"
57
EndProject
68
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleSample", "Samples\ConsoleSample\ConsoleSample.csproj", "{55D08A2C-1573-4F05-B229-0072DE16B4C1}"
@@ -9,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UtcSample", "Samples\UtcSam
911
EndProject
1012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BareSample", "Samples\BareSample\BareSample.csproj", "{B11F8506-624E-40A0-BA17-66069E32A946}"
1113
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataSample", "DataSample\DataSample.csproj", "{7CBF04DF-3FAB-4B4E-A4E0-5E6E3EA8EC8A}"
15+
EndProject
1216
Global
1317
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1418
Debug|Any CPU = Debug|Any CPU
@@ -31,6 +35,10 @@ Global
3135
{B11F8506-624E-40A0-BA17-66069E32A946}.Debug|Any CPU.Build.0 = Debug|Any CPU
3236
{B11F8506-624E-40A0-BA17-66069E32A946}.Release|Any CPU.ActiveCfg = Release|Any CPU
3337
{B11F8506-624E-40A0-BA17-66069E32A946}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{7CBF04DF-3FAB-4B4E-A4E0-5E6E3EA8EC8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{7CBF04DF-3FAB-4B4E-A4E0-5E6E3EA8EC8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{7CBF04DF-3FAB-4B4E-A4E0-5E6E3EA8EC8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{7CBF04DF-3FAB-4B4E-A4E0-5E6E3EA8EC8A}.Release|Any CPU.Build.0 = Release|Any CPU
3442
EndGlobalSection
3543
GlobalSection(SolutionProperties) = preSolution
3644
HideSolutionNode = FALSE

Eleven41.Logging/DataLog.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Eleven41.Logging
6+
{
7+
/// <summary>
8+
/// Wraps another ILog object and passes the supplied data
9+
/// with every message.
10+
/// </summary>
11+
public class DataLog : ILog
12+
{
13+
ILog _log;
14+
15+
public DataLog(ILog log, Dictionary<string, object> data)
16+
{
17+
_log = log;
18+
19+
if (data != null)
20+
_data = new Dictionary<string, object>(data);
21+
else
22+
_data = new Dictionary<string, object>();
23+
}
24+
25+
private Dictionary<string, object> _data;
26+
27+
public Dictionary<string, object> Data
28+
{
29+
get { return _data; }
30+
set
31+
{
32+
if (value == null)
33+
throw new ArgumentNullException("Data", "Data must not be null");
34+
_data = value;
35+
}
36+
}
37+
38+
public void Log(LogLevels level, string sFormat, params object[] args)
39+
{
40+
// Pass the message on including our extra data
41+
_log.Log(level, _data, sFormat, args);
42+
}
43+
44+
public void Log(LogLevels level, Dictionary<string, object> messageData, string sFormat, params object[] args)
45+
{
46+
// Start with our seed data
47+
Dictionary<string, object> data = new Dictionary<string, object>(_data);
48+
49+
// Add the message data
50+
if (messageData != null)
51+
{
52+
foreach (var kvp in messageData)
53+
data[kvp.Key] = kvp.Value;
54+
}
55+
56+
_log.Log(level, data, sFormat, args);
57+
}
58+
}
59+
}

Eleven41.Logging/Eleven41.Logging.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<Reference Include="System.Data" />
3838
</ItemGroup>
3939
<ItemGroup>
40+
<Compile Include="DataLog.cs" />
4041
<Compile Include="TextWriterFormatters\BareTextWriterFormatter.cs" />
4142
<Compile Include="ConsoleLog.cs" />
4243
<Compile Include="DailyFileLog.cs" />

Eleven41.Logging/Eleven41.Logging.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<package >
33
<metadata>
44
<id>Eleven41.Logging</id>
5-
<version>1.1.1</version>
5+
<version>1.2.0</version>
66
<authors>Eleven41 Software</authors>
77
<owners>Eleven41 Software</owners>
88
<licenseUrl>https://github.com/eleven41/Eleven41.Logging/blob/master/LICENSE.md</licenseUrl>
99
<projectUrl>https://github.com/eleven41/Eleven41.Logging</projectUrl>
1010
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1111
<description>Eleven41.Logging is a general purpose logging framework, capable of logging to many different outputs. Those include console, file, Windows event Log, multiple outputs. Various logging levels exist, including diagnostic, info, warning, and error.</description>
12-
<releaseNotes>Renamed UtcDateTimeFormatter to UtcDateTimeProvider.</releaseNotes>
12+
<releaseNotes>Added logging messages with data.</releaseNotes>
1313
<copyright>Copyright (C) 2014, Eleven41 Software Inc.</copyright>
1414
<tags>logging</tags>
1515
</metadata>

Eleven41.Logging/ILog.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Eleven41.Logging
45
{
@@ -14,5 +15,14 @@ public interface ILog
1415
/// <param name="sFormat">Message format to send.</param>
1516
/// <param name="args">Message arguments.</param>
1617
void Log(LogLevels level, string sFormat, params Object[] args);
18+
19+
/// <summary>
20+
/// Sends a message to the log, including extra data (if supported).
21+
/// </summary>
22+
/// <param name="level">Level of the message.</param>
23+
/// <param name="data">Data to include with the message.</param>
24+
/// <param name="sFormat">Message format to send.</param>
25+
/// <param name="args">Message arguments.</param>
26+
void Log(LogLevels level, Dictionary<string, object> data, string sFormat, params Object[] args);
1727
}
1828
}

Eleven41.Logging/LogBase.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,16 @@ public bool LogErrors
120120
/// </summary>
121121
/// <param name="level">Level of the message.</param>
122122
/// <param name="sMsg">Message to log.</param>
123-
public abstract void Log(LogLevels level, string sMsg, params Object[] args);
123+
public abstract void Log(LogLevels level, string sFormat, params Object[] args);
124+
125+
public virtual void Log(LogLevels level, System.Collections.Generic.Dictionary<string, object> data, string sFormat, params object[] args)
126+
{
127+
// These log types do not support extra data,
128+
// so simply call the basic log function
129+
Log(level, sFormat, args);
130+
}
124131

125132
#endregion
133+
126134
}
127135
}

0 commit comments

Comments
 (0)