Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SHSE committed Mar 22, 2013
0 parents commit 6df88e6
Show file tree
Hide file tree
Showing 85 changed files with 6,216 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .nuget/NuGet.Config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
151 changes: 151 additions & 0 deletions .nuget/NuGet.targets
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>

<!-- Enable the restore command to run before builds -->
<RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>

<!-- Property that enables building a package from a project -->
<BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>

<!-- Download NuGet.exe if it does not already exist -->
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
</PropertyGroup>

<ItemGroup Condition=" '$(PackageSources)' == '' ">
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
<!-- The official NuGet package source (https://nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
<!--
<PackageSource Include="https://nuget.org/api/v2/" />
<PackageSource Include="https://my-nuget-source/nuget/" />
-->
</ItemGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
<!-- Windows specific commands -->
<NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
<PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
<PackagesConfig>packages.config</PackagesConfig>
</PropertyGroup>

<PropertyGroup>
<!-- NuGet command -->
<NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\nuget.exe</NuGetExePath>
<PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>

<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>

<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>

<RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
<!-- Commands -->
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(RequireConsentSwitch) -solutionDir "$(SolutionDir) "</RestoreCommand>
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand>

<!-- We need to ensure packages are restored prior to assembly resolve -->
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
RestorePackages;
$(BuildDependsOn);
</BuildDependsOn>

<!-- Make the build depend on restore packages -->
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
$(BuildDependsOn);
BuildPackage;
</BuildDependsOn>
</PropertyGroup>

<Target Name="CheckPrerequisites">
<!-- Raise an error if we're unable to locate nuget.exe -->
<Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
<SetEnvironmentVariable EnvKey="VisualStudioVersion" EnvValue="$(VisualStudioVersion)" Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " />
<!--
Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
This effectively acts as a lock that makes sure that the download operation will only happen once and all
parallel builds will have to wait for it to complete.
-->
<MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT" />
</Target>

<Target Name="_DownloadNuGet">
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
</Target>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(RestoreCommand)"
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

<Exec Command="$(RestoreCommand)"
LogStandardErrorAsError="true"
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(BuildCommand)"
Condition=" '$(OS)' != 'Windows_NT' " />

<Exec Command="$(BuildCommand)"
LogStandardErrorAsError="true"
Condition=" '$(OS)' == 'Windows_NT' " />
</Target>

<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<OutputFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
OutputFilename = Path.GetFullPath(OutputFilename);
Log.LogMessage("Downloading latest version of NuGet.exe...");
WebClient webClient = new WebClient();
webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);
return true;
}
catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>

<UsingTask TaskName="SetEnvironmentVariable" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<EnvKey ParameterType="System.String" Required="true" />
<EnvValue ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
Environment.SetEnvironmentVariable(EnvKey, EnvValue, System.EnvironmentVariableTarget.Process);
}
catch {
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
Binary file added .nuget/nuget.exe
Binary file not shown.
106 changes: 106 additions & 0 deletions LogWatch.Tests/Formats/CsvFormatTests.cs
@@ -0,0 +1,106 @@
using System;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading;
using LogWatch.Formats;
using Xunit;

namespace LogWatch.Tests.Formats {
public class CsvFormatTests {
[Fact]
public void ReadsSegment() {
var stream = CreateStream("2012-03-05 13:56:12;warn;Program;\"Test message\";TextException");

var format = new CsvLogFormat {
ReadHeader = false,
Delimeter = ';',
FieldCount = 5
};

var subject = new ReplaySubject<RecordSegment>();

format.ReadSegments(subject, stream, CancellationToken.None).Wait();

subject.OnCompleted();

var segment = subject.ToEnumerable().FirstOrDefault();

Assert.Equal(0, segment.Offset);
Assert.Equal(stream.Length, segment.Length);
}

[Fact]
public void GuessesOptionsFromHeader() {
var stream1 = CreateStream("time;level;message;logger;exception");
var stream2 = CreateStream("time,level,message,logger,exception");

var format = new CsvLogFormat {ReadHeader = true};

format.ReadSegments(new Subject<RecordSegment>(), stream1, CancellationToken.None).Wait();

Assert.Equal(';', format.Delimeter);
Assert.Equal(5, format.FieldCount);
Assert.Equal(0, format.TimestampFieldIndex);
Assert.Equal(1, format.LevelFieldIndex);
Assert.Equal(2, format.MessageFieldIndex);
Assert.Equal(3, format.LoggerFieldIndex);
Assert.Equal(4, format.ExceptionFieldIndex);

format = new CsvLogFormat {ReadHeader = true};
format.ReadSegments(new Subject<RecordSegment>(), stream2, CancellationToken.None).Wait();

Assert.Equal(',', format.Delimeter);
}

[Fact]
public void ReadsSegmentWithQuotedField() {
var stream = CreateStream(
"time;level;message;logger;exception\r\n" +
"2012-01-01 00:00:00;Info;\"Quoted \r\n field \r \n\";Program;Exception");

var format = new CsvLogFormat();
var subject = new ReplaySubject<RecordSegment>();

format.ReadSegments(subject, stream, CancellationToken.None).Wait();

subject.OnCompleted();

var segment = subject.ToEnumerable().FirstOrDefault();

Assert.Equal(37, segment.Offset);
Assert.Equal(66, segment.Length);
}

[Fact]
public void DeserializesRecord() {
var bytes = Encoding.UTF8.GetBytes("2012-01-01 01:39:40;Info;\"Quoted \r\n field \r \n\";Program;Exception");

var format = new CsvLogFormat {
Delimeter = ';',
FieldCount = 5,
TimestampFieldIndex = 0,
LevelFieldIndex = 1,
MessageFieldIndex = 2,
LoggerFieldIndex = 3,
ExceptionFieldIndex = 4
};

var record = format.DeserializeRecord(new ArraySegment<byte>(bytes));

Assert.Equal(new DateTime(2012, 1, 1, 1, 39, 40), record.Timestamp);
Assert.Equal(LogLevel.Info, record.Level);
Assert.Equal("Quoted \r\n field \r \n", record.Message);
Assert.Equal("Program", record.Logger);
Assert.Equal("Exception", record.Exception);
}

private static MemoryStream CreateStream(string content) {
var bytes = Encoding.UTF8.GetBytes(content);
var stream = new MemoryStream(bytes);
return stream;
}
}
}
70 changes: 70 additions & 0 deletions LogWatch.Tests/Formats/Log4JXmlFormatTests.cs
@@ -0,0 +1,70 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using LogWatch.Formats;
using Microsoft.Reactive.Testing;
using Xunit;

namespace LogWatch.Tests.Formats {
public class Log4JXmlFormatTests {
[Fact]
public void ReadsSegments() {
var bytes = Encoding.UTF8.GetBytes(
"<log4j:event logger=\"ConsoleApplication1.Program\" level=\"INFO\" timestamp=\"1361281966733\" thread=\"1\">" +
" <log4j:message>Istcua orojurf bysgurnl t.</log4j:message>" +
" <log4j:properties>" +
" <log4j:data name=\"log4japp\" value=\"ConsoleApplication1.exe(6512)\" />" +
" <log4j:data name=\"log4jmachinename\" value=\"user1\" />" +
" </log4j:properties>" +
"</log4j:event>" +
"<log4j:event logger=\"ConsoleApplication1.Program\" level=\"WARN\" timestamp=\"1361281966808\" thread=\"1\">" +
" <log4j:message>Ebo ohow aco inldrfb pameenegy.</log4j:message>" +
" <log4j:properties>" +
" <log4j:data name=\"log4japp\" value=\"ConsoleApplication1.exe(6512)\" />" +
" <log4j:data name=\"log4jmachinename\" value=\"user2\" />" +
" </log4j:properties>" +
"</log4j:event>");

var stream = new MemoryStream(bytes);
var format = new Log4JXmlLogFormat();
var testScheduler = new TestScheduler();
var observer = testScheduler.CreateObserver<RecordSegment>();

var offset = format.ReadSegments(observer, stream, CancellationToken.None).Result;

Assert.Equal(2, observer.Messages.Count);

var segments = observer.Messages.Select(x => x.Value.Value).ToArray();

Assert.True(segments.Any(x => x.Offset == 0 && x.Length == 342));
Assert.True(segments.Any(x => x.Offset == 342 && x.Length == 347));
Assert.Equal(stream.Length, offset);
}

[Fact]
public void DeserializesRecord() {
var bytes = Encoding.UTF8.GetBytes(
"<log4j:event logger=\"ConsoleApplication1.Program\" level=\"INFO\" timestamp=\"1361281966733\" thread=\"1\">" +
" <log4j:message>Istcua orojurf bysgurnl t.</log4j:message>" +
" <log4j:properties>" +
" <log4j:data name=\"log4japp\" value=\"ConsoleApplication1.exe(6512)\" />" +
" <log4j:data name=\"log4jmachinename\" value=\"user1\" />" +
" <log4j:data name=\"exception\" value=\"TestException\" />" +
" </log4j:properties>" +
"</log4j:event>");

var format = new Log4JXmlLogFormat();

var record = format.DeserializeRecord(new ArraySegment<byte>(bytes));

Assert.Equal(LogLevel.Info, record.Level);
Assert.Equal("Istcua orojurf bysgurnl t.", record.Message);
Assert.Equal("ConsoleApplication1.Program", record.Logger);
Assert.Equal("1", record.Thread);
Assert.Equal("TestException", record.Exception);
Assert.Equal(new DateTime(2013, 02, 19, 13, 52, 47), record.Timestamp);
}
}
}

0 comments on commit 6df88e6

Please sign in to comment.