added SimpleLog.Tests project and unit test for DefaultMessageHandler
This commit is contained in:
parent
613eda62c2
commit
65937ec510
44
SimpleLog.Tests/DefaultMessageHandlerTest.cs
Normal file
44
SimpleLog.Tests/DefaultMessageHandlerTest.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NUnit.Framework.SyntaxHelpers;
|
||||||
|
|
||||||
|
namespace SimpleLog.Tests {
|
||||||
|
[TestFixture]
|
||||||
|
public class DefaultMessageHandlerTest {
|
||||||
|
|
||||||
|
private DefaultMessageHandler messageHandler;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Init() {
|
||||||
|
this.messageHandler = new DefaultMessageHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestConvertMessageToStringWithException() {
|
||||||
|
Exception e = new Exception("yay!");
|
||||||
|
Assert.That(this.messageHandler.ConvertMessageToString(e), Text.StartsWith("yay!\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestConvertMessageToString() {
|
||||||
|
string message = "yay!";
|
||||||
|
Assert.That(this.messageHandler.ConvertMessageToString(message), Text.Matches("yay!"));
|
||||||
|
object obj = "yay!";
|
||||||
|
Assert.That(this.messageHandler.ConvertMessageToString(obj), Text.Matches("yay!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestConstructLogMessage() {
|
||||||
|
this.messageHandler.DateFormat = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
this.messageHandler.Delimiter = " ";
|
||||||
|
this.messageHandler.Context = "some kind of context";
|
||||||
|
string expected = "\\d{4}-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d ";
|
||||||
|
expected += "WARNING ";
|
||||||
|
expected += "\\[some kind of context\\] ";
|
||||||
|
expected += "log message\n";
|
||||||
|
|
||||||
|
Assert.That(this.messageHandler.ConstructLogMessage("log message", LogLevel.Warning), Text.Matches(expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
36
SimpleLog.Tests/Properties/AssemblyInfo.cs
Normal file
36
SimpleLog.Tests/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
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("SimpleLog.Tests")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("Bridgepoint Education")]
|
||||||
|
[assembly: AssemblyProduct("SimpleLog.Tests")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © Bridgepoint Education 2009")]
|
||||||
|
[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("36a0db9c-388f-41a7-a278-0aafa72986bb")]
|
||||||
|
|
||||||
|
// 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")]
|
68
SimpleLog.Tests/SimpleLog.Tests.csproj
Normal file
68
SimpleLog.Tests/SimpleLog.Tests.csproj
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProductVersion>9.0.30729</ProductVersion>
|
||||||
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
|
<ProjectGuid>{4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>SimpleLog.Tests</RootNamespace>
|
||||||
|
<AssemblyName>SimpleLog.Tests</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
</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>
|
||||||
|
<Reference Include="nunit.core, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
|
||||||
|
<Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
|
||||||
|
<Reference Include="nunit.mocks, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core">
|
||||||
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Xml.Linq">
|
||||||
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Data.DataSetExtensions">
|
||||||
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="DefaultMessageHandlerTest.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\SimpleLog\SimpleLog.csproj">
|
||||||
|
<Project>{20AC889F-0D03-453F-8759-4D1887F3BC0C}</Project>
|
||||||
|
<Name>SimpleLog</Name>
|
||||||
|
</ProjectReference>
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.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>
|
@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 10.00
|
|||||||
# Visual C# Express 2008
|
# Visual C# Express 2008
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLog", "SimpleLog\SimpleLog.csproj", "{20AC889F-0D03-453F-8759-4D1887F3BC0C}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLog", "SimpleLog\SimpleLog.csproj", "{20AC889F-0D03-453F-8759-4D1887F3BC0C}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLog.Tests", "SimpleLog.Tests\SimpleLog.Tests.csproj", "{4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -13,6 +15,10 @@ Global
|
|||||||
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Release|Any CPU.Build.0 = Release|Any CPU
|
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{4ECCF9B6-8A33-4D17-97EA-BA2D9D9178DD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -51,7 +51,7 @@ namespace SimpleLog {
|
|||||||
messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat;
|
messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat;
|
||||||
|
|
||||||
convertedMessage = messageHandler.ConvertMessageToString(message);
|
convertedMessage = messageHandler.ConvertMessageToString(message);
|
||||||
convertedMessage = messageHandler.ConstructLogMessage(handler, convertedMessage, level);
|
convertedMessage = messageHandler.ConstructLogMessage(convertedMessage, level);
|
||||||
|
|
||||||
success = (success && handler.Log(convertedMessage, allowedLevel));
|
success = (success && handler.Log(convertedMessage, allowedLevel));
|
||||||
}
|
}
|
||||||
|
@ -22,12 +22,12 @@ namespace SimpleLog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region IMessageHandler Members
|
#region IMessageHandler Members
|
||||||
public string ConstructLogMessage(ILogHandler handler, string message, LogLevel level) {
|
public string ConstructLogMessage(string message, LogLevel level) {
|
||||||
List<string> messageData = new List<string>();
|
List<string> messageData = new List<string>();
|
||||||
messageData.Add(string.Format("{0:" + this.DateFormat + "}", DateTime.Now));
|
messageData.Add(string.Format("{0:" + this.DateFormat + "}", DateTime.Now));
|
||||||
messageData.Add(level.ToString().ToUpper());
|
messageData.Add(level.ToString().ToUpper());
|
||||||
if (!string.IsNullOrEmpty(this.Context)) {
|
if (!string.IsNullOrEmpty(this.Context)) {
|
||||||
messageData.Add(this.Context);
|
messageData.Add("[" + this.Context + "]");
|
||||||
}
|
}
|
||||||
messageData.Add(message);
|
messageData.Add(message);
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
namespace SimpleLog {
|
namespace SimpleLog {
|
||||||
public interface IMessageHandler : IMessageFormatter {
|
public interface IMessageHandler : IMessageFormatter {
|
||||||
|
|
||||||
string ConstructLogMessage(ILogHandler handler, string message, LogLevel level);
|
string ConstructLogMessage(string message, LogLevel level);
|
||||||
string ConvertMessageToString(object message);
|
string ConvertMessageToString(object message);
|
||||||
string Context { get; set; }
|
string Context { get; set; }
|
||||||
|
|
||||||
|
@ -6,11 +6,11 @@ using System.Runtime.InteropServices;
|
|||||||
// set of attributes. Change these attribute values to modify the information
|
// set of attributes. Change these attribute values to modify the information
|
||||||
// associated with an assembly.
|
// associated with an assembly.
|
||||||
[assembly: AssemblyTitle("SimpleLog")]
|
[assembly: AssemblyTitle("SimpleLog")]
|
||||||
[assembly: AssemblyDescription("")]
|
[assembly: AssemblyDescription("Simple .NET 3.5 logging utility")]
|
||||||
[assembly: AssemblyConfiguration("")]
|
[assembly: AssemblyConfiguration("")]
|
||||||
[assembly: AssemblyCompany("Bridgepoint Education")]
|
[assembly: AssemblyCompany("")]
|
||||||
[assembly: AssemblyProduct("SimpleLog")]
|
[assembly: AssemblyProduct("SimpleLog")]
|
||||||
[assembly: AssemblyCopyright("Copyright © Bridgepoint Education 2009")]
|
[assembly: AssemblyCopyright("Copyright © Tommy Montgomery 2009")]
|
||||||
[assembly: AssemblyTrademark("")]
|
[assembly: AssemblyTrademark("")]
|
||||||
[assembly: AssemblyCulture("")]
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user