Skip to content

Commit

Permalink
Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jjxtra committed Mar 28, 2012
1 parent c0d492b commit b282923
Show file tree
Hide file tree
Showing 23 changed files with 89,037 additions and 8 deletions.
5 changes: 5 additions & 0 deletions IPBan.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="NLog">
<HintPath>packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
Expand All @@ -54,12 +57,14 @@
<Compile Include="IPBanService.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Logger.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
<None Include="README" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
21 changes: 13 additions & 8 deletions IPBanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void ClearBannedIP()

private void ProcessXml(string xml)
{
Console.WriteLine("Processing xml: {0}", xml);
Log.Write(LogLevel.Info, "Processing xml: {0}", xml);

string ipAddress = null;
XmlTextReader reader = new XmlTextReader(new StringReader(xml));
Expand All @@ -125,15 +125,15 @@ private void ProcessXml(string xml)

if (nodes.Count == 0)
{
Console.WriteLine("No nodes found for xpath {0}", expression.XPath);
Log.Write(LogLevel.Warning, "No nodes found for xpath {0}", expression.XPath);
ipAddress = null;
break;
}

// if there is a regex, it must match
if (expression.Regex.Length == 0)
{
Console.WriteLine("No regex, so counting as a match");
Log.Write(LogLevel.Info, "No regex, so counting as a match");
}
else
{
Expand All @@ -159,7 +159,7 @@ private void ProcessXml(string xml)

if (!foundMatch)
{
Console.WriteLine("Regex {0} did not match any nodes with xpath {1}", expression.Regex, expression.XPath);
Log.Write(LogLevel.Warning, "Regex {0} did not match any nodes with xpath {1}", expression.Regex, expression.XPath);
ipAddress = null;
break;
}
Expand Down Expand Up @@ -191,10 +191,10 @@ private void ProcessXml(string xml)
ipBlocker.TryGetValue(ipAddress, out count);
count++;
ipBlocker[ipAddress] = count;
Console.WriteLine("Got event with ip address {0}, count: {1}", ipAddress, count);
Log.Write(LogLevel.Info, "Got event with ip address {0}, count: {1}", ipAddress, count);
if (count == failedLoginAttemptsBeforeBan)
{
Console.WriteLine("Banning ip address {0}", ipAddress);
Log.Write(LogLevel.Error, "Banning ip address {0}", ipAddress);
Process.Start("netsh", "advfirewall firewall add rule \"name=" + rulePrefix + ipAddress + "\" dir=in protocol=any action=block remoteip=" + ipAddress);
File.AppendAllText(banFile, ipAddress + Environment.NewLine);
ipBlockerDate[ipAddress] = DateTime.UtcNow;
Expand All @@ -203,7 +203,7 @@ private void ProcessXml(string xml)
}
else
{
Console.WriteLine("Skipping ip address '{0}'", ipAddress);
Log.Write(LogLevel.Info, "Skipping ip address '{0}'", ipAddress);
}
}
}
Expand Down Expand Up @@ -337,7 +337,7 @@ private void CheckForExpiredIP()

if (elapsed.Days > 0)
{
Console.WriteLine("Un-banning ip address {0}", keyValue.Key);
Log.Write(LogLevel.Error, "Un-banning ip address {0}", keyValue.Key);
Process.Start("netsh", "advfirewall firewall delete rule \"name=" + rulePrefix + keyValue.Key + "\"");
lock (ipBlocker)
{
Expand Down Expand Up @@ -379,6 +379,7 @@ protected override void OnStart(string[] args)
{
base.OnStart(args);

Log.Write(LogLevel.Info, "Started IPBan service");
run = true;
serviceThread = new Thread(new ThreadStart(ServiceThread));
serviceThread.Start();
Expand All @@ -391,6 +392,8 @@ protected override void OnStop()
run = false;
query = null;
watcher = null;

Log.Write(LogLevel.Info, "Stopped IPBan service");
}

public static void RunService(string[] args)
Expand All @@ -411,6 +414,8 @@ public static void RunConsole(string[] args)

public static void Main(string[] args)
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

if (args.Length != 0 && args[0] == "debug")
{
RunConsole(args);
Expand Down
42 changes: 42 additions & 0 deletions Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NLog;

namespace IPBan
{
public enum LogLevel
{
Info,
Warning,
Error
}

/// <summary>
/// Logger
/// </summary>
public static class Log
{
private static readonly Logger logger = LogManager.GetLogger("FileLogger");

public static void Write(LogLevel level, string text, params object[] args)
{
switch (level)
{
case LogLevel.Info:
logger.Info(text, args);
break;

case LogLevel.Warning:
logger.Warn(text, args);
break;

case LogLevel.Error:
logger.Error(text, args);
break;
}
}
}
}
12 changes: 12 additions & 0 deletions app.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<configuration>
<configSections>
<section name="ExpressionsToBlock" type="IPBan.ExpressionsToBlockConfigSectionHandler, IPBan" />
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>

<!--
Expand Down Expand Up @@ -47,6 +48,17 @@
</Group>
</Groups>
</ExpressionsToBlock>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="logfile.txt"/>
<target name="console" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>

<appSettings>

Expand Down
4 changes: 4 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NLog" version="2.0.0.2000" />
</packages>
Binary file added packages/NLog.2.0.0.2000/NLog.2.0.0.2000.nupkg
Binary file not shown.
Binary file added packages/NLog.2.0.0.2000/lib/net20/NLog.dll
Binary file not shown.
Loading

0 comments on commit b282923

Please sign in to comment.