Skip to content

Commit

Permalink
Migrated code from PR branch aspnet/Logging#182
Browse files Browse the repository at this point in the history
 * Changed name/namespace from Microsoft.Framework.Logging.Serilog to Serilog.Framework.Logging
 * Switched tests to NUnit (in line with Serilog)
 * Tweaked the sample to (optimistically) run when DNXCORE50 is targeted
  • Loading branch information
nblumhardt committed May 15, 2015
1 parent 7054dda commit 4ebe6d7
Show file tree
Hide file tree
Showing 17 changed files with 2,636 additions and 24 deletions.
70 changes: 70 additions & 0 deletions samples/Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using Microsoft.Framework.Logging;
using ILogger = Microsoft.Framework.Logging.ILogger;
using Serilog;

namespace Sample
{
public class Program
{
private readonly ILogger _logger;

public Program()
{
var factory = new LoggerFactory();
_logger = factory.CreateLogger(typeof(Program).FullName);
#if DNXCORE50
factory.AddSerilog(new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.TextWriter(Console.Out));
#else
factory.AddSerilog(new LoggerConfiguration()
.Enrich.WithMachineName()
.Enrich.WithProcessId()
.Enrich.WithThreadId()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole());
#endif
}

public void Main(string[] args)
{
_logger.LogInformation("Starting");

var startTime = DateTimeOffset.UtcNow;
_logger.LogInformation(1, "Started at '{StartTime}' and 0x{Hello:X} is hex of 42", startTime, 42);

try
{
throw new Exception("Boom");
}
catch (Exception ex)
{
_logger.LogCritical("Unexpected critical error starting application", ex);
_logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null);
// This write should not log anything
_logger.Log(LogLevel.Critical, 0, null, null, null);
_logger.LogError("Unexpected error", ex);
_logger.LogWarning("Unexpected warning", ex);
}

using (_logger.BeginScope("Main"))
{
Console.WriteLine("Hello World");

_logger.LogInformation("Waiting for user input");
Console.ReadLine();
}

var endTime = DateTimeOffset.UtcNow;
_logger.LogInformation(2, "Stopping at '{StopTime}'", endTime);

_logger.LogInformation("Stopping");

_logger.LogInformation(Environment.NewLine);
_logger.LogInformation("{Result,-10}{StartTime,15}{EndTime,15}{Duration,15}", "RESULT", "START TIME", "END TIME", "DURATION(ms)");
_logger.LogInformation("{Result,-10}{StartTime,15}{EndTime,15}{Duration,15}", "------", "----- ----", "--- ----", "------------");
_logger.LogInformation("{Result,-10}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds);
}
}
}
20 changes: 20 additions & 0 deletions samples/Sample/Sample.xproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>

<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>65357fbc-9bc4-466d-b621-1c3a19bc2a78</ProjectGuid>
<RootNamespace>Sample</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>

<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
33 changes: 33 additions & 0 deletions samples/Sample/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"version": "1.0.0-*",
"description": "",
"authors": [ "" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",

"dependencies": {
"Microsoft.Framework.Logging": "1.0.0-*",
"Serilog.Framework.Logging": "1.0.0-*",
"Serilog.Sinks.Literate": "1.0.6"
},

"commands": {
"Sample": "Sample"
},

"frameworks": {
"dnx451": {
"Serilog.Framework.Logging": "1.0.0-*"
},
"dnxcore50": {
"dependencies": {
"System.Console": "4.0.0-beta-22816",
"System.Collections": "4.0.10-beta-22816",
"System.Linq": "4.0.0-beta-22816",
"System.Threading": "4.0.10-beta-22816",
"Microsoft.CSharp": "4.0.0-beta-22816"
}
}
}
}
Loading

0 comments on commit 4ebe6d7

Please sign in to comment.