Skip to content

Commit 3681fad

Browse files
Merge pull request #6 from zippy1981/log4net
Added a Log4net appender
2 parents 3a7d37d + 81ddd14 commit 3681fad

File tree

12 files changed

+244
-9
lines changed

12 files changed

+244
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ publish/
132132

133133
# NuGet Packages Directory
134134
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
135-
#packages/
135+
packages/
136136

137137
# Windows Azure Build Output
138138
csx

Source/ManagedTest/App.config

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3+
<configSections>
4+
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
5+
</configSections>
36
<system.diagnostics>
47
<trace autoflush="true" indentsize="0">
58
<listeners>
69
<add name="SysInternalsListener" type="Sysinternals.Debug.ProcessMonitorTraceListener, Sysinternals.Debug"/>
710
</listeners>
811
</trace>
912
</system.diagnostics>
10-
<startup>
11-
12-
<supportedRuntime version="v2.0.50727"/></startup>
13+
<log4net>
14+
<appender name="ProcMon" type="Sysinternals.log4net.ProcMonAppender, Sysinternals.log4net">
15+
<layout type="log4net.Layout.PatternLayout">
16+
<conversionPattern value="%-5level %message" />
17+
</layout>
18+
</appender>
19+
20+
<root>
21+
<level value="DEBUG" />
22+
<appender-ref ref="ProcMon" />
23+
</root>
24+
</log4net>
25+
1326
</configuration>

Source/ManagedTest/ManagedTest.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
<UseVSHostingProcess>false</UseVSHostingProcess>
8282
</PropertyGroup>
8383
<ItemGroup>
84+
<Reference Include="log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
85+
<SpecificVersion>False</SpecificVersion>
86+
<HintPath>..\packages\log4net.2.0.3\lib\net20-full\log4net.dll</HintPath>
87+
</Reference>
8488
<Reference Include="System" />
8589
</ItemGroup>
8690
<ItemGroup>
@@ -89,6 +93,13 @@
8993
</ItemGroup>
9094
<ItemGroup>
9195
<None Include="App.config" />
96+
<None Include="packages.config" />
97+
</ItemGroup>
98+
<ItemGroup>
99+
<ProjectReference Include="..\Sysinternals.log4net\Sysinternals.log4net.csproj">
100+
<Project>{b276cb46-42a1-4cf8-a0bd-7de40230764e}</Project>
101+
<Name>Sysinternals.log4net</Name>
102+
</ProjectReference>
92103
</ItemGroup>
93104
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
94105
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Source/ManagedTest/Program.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,54 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Reflection;
34
using System.Text;
45
using System.Diagnostics;
56

7+
using log4net;
8+
using log4net.Appender;
9+
using log4net.Core;
10+
using log4net.Repository.Hierarchy;
11+
using Sysinternals.log4net;
12+
613
namespace ManagedTest
714
{
815
class Program
916
{
1017
static void Main(string[] args)
1118
{
19+
/*
20+
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
21+
hierarchy.Root.AddAppender(new ColoredConsoleAppender());
22+
hierarchy.Root.AddAppender(new ProcMonAppender());
23+
hierarchy.Root.Level = Level.Debug;
24+
hierarchy.Configured = true;
25+
*/
26+
ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
1227
Debug.Listeners.Remove("Default");
1328

1429
StringBuilder sb = new StringBuilder(100);
1530
for (int i = 0; i < 20; i++)
1631
{
1732
sb.Length = 0;
18-
sb.AppendFormat("ProcMon Debug Out Test # {0}",
19-
i.ToString());
33+
sb.AppendFormat("ProcMon Debug Out Test # {0}", i);
2034
Trace.Write(sb.ToString());
35+
36+
if (i%2 == 0)
37+
{
38+
_logger.DebugFormat("ProcMon log4net Out Test # {0}", i);
39+
}
40+
else if (i%3 == 0)
41+
{
42+
_logger.InfoFormat("ProcMon log4net Out Test # {0}", i);
43+
}
44+
else if (i%5 == 0)
45+
{
46+
_logger.WarnFormat("ProcMon log4net Out Test # {0}", i);
47+
}
48+
else
49+
{
50+
_logger.ErrorFormat("ProcMon log4net Out Test # {0}", i);
51+
}
2152
}
2253
}
2354
}

Source/ManagedTest/Properties/AssemblyInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// General Information about an assembly is controlled through the following
66
// set of attributes. Change these attribute values to modify the information
77
// associated with an assembly.
8+
using log4net.Config;
9+
810
[assembly: AssemblyTitle("ManagedTest")]
911
[assembly: AssemblyDescription("")]
1012
[assembly: AssemblyConfiguration("")]
@@ -34,3 +36,6 @@
3436
// [assembly: AssemblyVersion("1.0.*")]
3537
[assembly: AssemblyVersion("1.0.0.0")]
3638
[assembly: AssemblyFileVersion("1.0.0.0")]
39+
40+
// Read the log4net config from the app.config
41+
[assembly:XmlConfigurator(Watch = false)]

Source/ManagedTest/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="log4net" version="2.0.3" targetFramework="net20" />
4+
</packages>

Source/ProcMonDebugOutput.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sysinternals.Debug", "Sysin
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagedTest", "ManagedTest\ManagedTest.csproj", "{8D834016-ED7D-416C-B894-259F5BDF6CC6}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sysinternals.log4net", "Sysinternals.log4net\Sysinternals.log4net.csproj", "{B276CB46-42A1-4CF8-A0BD-7DE40230764E}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -99,6 +101,20 @@ Global
99101
{8D834016-ED7D-416C-B894-259F5BDF6CC6}.Release|x64.Build.0 = Release|x64
100102
{8D834016-ED7D-416C-B894-259F5BDF6CC6}.Release|x86.ActiveCfg = Release|x86
101103
{8D834016-ED7D-416C-B894-259F5BDF6CC6}.Release|x86.Build.0 = Release|x86
104+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
105+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Debug|Any CPU.Build.0 = Debug|Any CPU
106+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
107+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
108+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Debug|Win32.ActiveCfg = Debug|Any CPU
109+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Debug|x64.ActiveCfg = Debug|Any CPU
110+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Debug|x86.ActiveCfg = Debug|Any CPU
111+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Release|Any CPU.ActiveCfg = Release|Any CPU
112+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Release|Any CPU.Build.0 = Release|Any CPU
113+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
114+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Release|Mixed Platforms.Build.0 = Release|Any CPU
115+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Release|Win32.ActiveCfg = Release|Any CPU
116+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Release|x64.ActiveCfg = Release|Any CPU
117+
{B276CB46-42A1-4CF8-A0BD-7DE40230764E}.Release|x86.ActiveCfg = Release|Any CPU
102118
EndGlobalSection
103119
GlobalSection(SolutionProperties) = preSolution
104120
HideSolutionNode = FALSE

Source/Sysinternals.Debug/NativeMethods.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ internal static class NativeMethods
3636
/// <param name="message">
3737
/// The message to display.
3838
/// </param>
39+
/// <param name="args">
40+
/// The formatting arguments for the message
41+
/// </param>
3942
/// <returns>
4043
/// True if the trace succeeded, false otherwise.
4144
/// </returns>
42-
public static bool ProcMonDebugOutput(string message)
45+
public static bool ProcMonDebugOutput(string message, params object[] args)
4346
{
4447
if (false == lookedUpProcessType)
4548
{
@@ -56,13 +59,14 @@ public static bool ProcMonDebugOutput(string message)
5659
bool returnValue = false;
5760
try
5861
{
62+
string renderedMessage = string.Format(message, args);
5963
if (true == is64BitProcess)
6064
{
61-
returnValue = ProcMonDebugOutputx64(message);
65+
returnValue = ProcMonDebugOutputx64(renderedMessage);
6266
}
6367
else
6468
{
65-
returnValue = ProcMonDebugOutputWin32(message);
69+
returnValue = ProcMonDebugOutputWin32(renderedMessage);
6670
}
6771
}
6872
catch (EntryPointNotFoundException notFoundException)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using log4net.Appender;
3+
using log4net.Core;
4+
using log4net.Layout;
5+
using Sysinternals.Debug;
6+
7+
namespace Sysinternals.log4net
8+
{
9+
/// <summary>
10+
/// A <c>log4net</c> appender for ProcMonDebugOutput.
11+
/// </summary>
12+
public class ProcMonAppender : AppenderSkeleton
13+
{
14+
/// <summary>
15+
/// Default constructor.
16+
/// </summary>
17+
/// <remarks>
18+
/// Sets the default layout.
19+
/// </remarks>
20+
public ProcMonAppender()
21+
{
22+
// Although it breaks convention set by the built-in appenders, this is more forgiving.
23+
Layout = new PatternLayout("%-5p %m");
24+
}
25+
/// <summary>
26+
/// This appender requires a <see cref="AppenderSkeleton.Layout"/> to be set.
27+
/// </summary>
28+
/// <value><c>true</c></value>
29+
override protected bool RequiresLayout
30+
{
31+
get { return true; }
32+
}
33+
34+
/// <summary>
35+
///
36+
/// </summary>
37+
/// <param name="loggingEvent"></param>
38+
protected override void Append(LoggingEvent loggingEvent)
39+
{
40+
NativeMethods.ProcMonDebugOutput(RenderLoggingEvent(loggingEvent));
41+
}
42+
}
43+
}
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("Sysinternals.log4net")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Sysinternals.log4net")]
13+
[assembly: AssemblyCopyright("Copyright © 2014")]
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("d8560003-f6b2-40b4-989f-ff24d28a8eb4")]
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")]

0 commit comments

Comments
 (0)