Basically works the way I want.
parent
ae98510d99
commit
8b5e9dbc76
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Diagnostics;
|
||||
using Mono.Options;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
namespace EventPump
|
||||
{
|
||||
class EventPump
|
||||
{
|
||||
static ConcurrentQueue<EventLogEntry> events = new ConcurrentQueue<EventLogEntry>();
|
||||
|
||||
volatile bool RunCollect;
|
||||
static List<string> logs = new List<string>();
|
||||
static List<int> event_ids = new List<int>();
|
||||
static List<EventLog> watching_logs = new List<EventLog>();
|
||||
static int verbose = 0;
|
||||
static bool showHelp = false;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var p = new OptionSet()
|
||||
.Add("v", "Verbosely print internal events.", v => ++verbose)
|
||||
.Add("log=", "Specify log to collect from, may be used multiple times.", l => logs.Add(l))
|
||||
.Add("id=", "Comma separated list of event IDs to filter on", id => {
|
||||
string[] elements = id.Split(',');
|
||||
foreach (var i in elements) {
|
||||
int x = -1;
|
||||
int.TryParse(i, out x);
|
||||
if (x > 0) event_ids.Add(x);
|
||||
}
|
||||
})
|
||||
.Add("h|?|help", "Show this help.", v => showHelp = true);
|
||||
p.Parse(args);
|
||||
|
||||
if (showHelp) {
|
||||
var helpText = @"Usage: EventPump.exe -log Application -id 63,25
|
||||
the -id option may contain multiple values separated by
|
||||
commas but no whitespace.
|
||||
";
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(helpText);
|
||||
Console.WriteLine();
|
||||
p.WriteOptionDescriptions(Console.Out);
|
||||
Console.WriteLine();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
foreach (var l in logs) Console.WriteLine("Log: {0}", l);
|
||||
foreach (var i in event_ids) Console.WriteLine("ID's: {0}", i);
|
||||
#endif
|
||||
|
||||
foreach (var l in logs) {
|
||||
var el = new EventLog(l);
|
||||
el.EntryWritten += (s, e) => {
|
||||
if (event_ids.Any(x => x == e.Entry.InstanceId || x == e.Entry.EventID)) {
|
||||
events.Enqueue(e.Entry);
|
||||
} else if (event_ids.Count == 0) {
|
||||
events.Enqueue(e.Entry);
|
||||
}
|
||||
};
|
||||
el.EnableRaisingEvents = true;
|
||||
watching_logs.Add(el);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
if (events.Count > 0) {
|
||||
EventLogEntry e;
|
||||
events.TryDequeue(out e);
|
||||
if (e != null) {
|
||||
Dictionary<string, dynamic> eventEntry = new Dictionary<string, dynamic>(); ;
|
||||
eventEntry.Add("TimeGenerated", e.TimeGenerated);
|
||||
eventEntry.Add("EventID", e.EventID);
|
||||
eventEntry.Add("InstanceID", e.InstanceId);
|
||||
eventEntry.Add("EntryType", e.EntryType);
|
||||
eventEntry.Add("EventSource", e.Source);
|
||||
eventEntry.Add("MachineName", e.MachineName);
|
||||
eventEntry.Add("UserName", e.UserName);
|
||||
eventEntry.Add("Message", e.Message);
|
||||
Console.WriteLine(JsonConvert.SerializeObject(eventEntry));
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.Threading.Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,6 +11,8 @@
|
|||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
|
@ -32,6 +34,10 @@
|
|||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Costura, Version=1.6.2.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Costura.Fody.1.6.2\lib\dotnet\Costura.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
|
@ -46,12 +52,29 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Options.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="EventPump.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="FodyWeavers.xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Resource Include="..\packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll">
|
||||
<Link>Newtonsoft.Json.dll</Link>
|
||||
</Resource>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="..\packages\Fody.2.0.0\build\dotnet\Fody.targets" Condition="Exists('..\packages\Fody.2.0.0\build\dotnet\Fody.targets')" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\Fody.2.0.0\build\dotnet\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.2.0.0\build\dotnet\Fody.targets'))" />
|
||||
<Error Condition="!Exists('..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets'))" />
|
||||
</Target>
|
||||
<Import Project="..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets" Condition="Exists('..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets')" />
|
||||
</Project>
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Weavers>
|
||||
<Costura />
|
||||
</Weavers>
|
|
@ -1,88 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Diagnostics;
|
||||
using Mono.Options;
|
||||
|
||||
|
||||
namespace EventPump
|
||||
{
|
||||
class Program
|
||||
{
|
||||
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
|
||||
Console.WriteLine("Press Enter");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class EventCollector
|
||||
{
|
||||
volatile bool RunCollect = true;
|
||||
ConcurrentQueue<EventLog> events = new ConcurrentQueue<EventLog>();
|
||||
List<string> logs = new List<string>();
|
||||
List<int> event_ids = new List<int>();
|
||||
CollectorStatus status;
|
||||
int verbose = 0;
|
||||
|
||||
|
||||
EventCollector(string[] args)
|
||||
{
|
||||
status = CollectorStatus.starting;
|
||||
|
||||
var p = new OptionSet()
|
||||
.Add("v", v => ++verbose)
|
||||
.Add("log=", l => logs.Add(l))
|
||||
.Add("id=", id =>
|
||||
{
|
||||
string[] elements = id.Split(',');
|
||||
foreach (var i in elements)
|
||||
{
|
||||
int x = -1;
|
||||
int.TryParse(i, out x);
|
||||
if (x > 0) event_ids.Add(x);
|
||||
}
|
||||
});
|
||||
p.Parse(args);
|
||||
}
|
||||
|
||||
|
||||
void Start() {
|
||||
status = CollectorStatus.running;
|
||||
|
||||
Task.Run(() => {
|
||||
while (RunCollect)
|
||||
{
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void Stop() {
|
||||
RunCollect = false;
|
||||
}
|
||||
|
||||
|
||||
void Pause() {
|
||||
status = CollectorStatus.paused;
|
||||
}
|
||||
}
|
||||
|
||||
enum CollectorStatus
|
||||
{
|
||||
running,
|
||||
paused,
|
||||
starting,
|
||||
stopping,
|
||||
}
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Costura.Fody" version="1.6.2" targetFramework="net452" developmentDependency="true" />
|
||||
<package id="Fody" version="2.0.0" targetFramework="net452" developmentDependency="true" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
|
||||
</packages>
|
Loading…
Reference in New Issue