Skip to content

Commit 4ebe6d7

Browse files
committed
Migrated code from PR branch aspnet/Logging#182
* 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
1 parent 7054dda commit 4ebe6d7

17 files changed

+2636
-24
lines changed

samples/Sample/Program.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using Microsoft.Framework.Logging;
3+
using ILogger = Microsoft.Framework.Logging.ILogger;
4+
using Serilog;
5+
6+
namespace Sample
7+
{
8+
public class Program
9+
{
10+
private readonly ILogger _logger;
11+
12+
public Program()
13+
{
14+
var factory = new LoggerFactory();
15+
_logger = factory.CreateLogger(typeof(Program).FullName);
16+
#if DNXCORE50
17+
factory.AddSerilog(new LoggerConfiguration()
18+
.MinimumLevel.Debug()
19+
.WriteTo.TextWriter(Console.Out));
20+
#else
21+
factory.AddSerilog(new LoggerConfiguration()
22+
.Enrich.WithMachineName()
23+
.Enrich.WithProcessId()
24+
.Enrich.WithThreadId()
25+
.MinimumLevel.Debug()
26+
.WriteTo.LiterateConsole());
27+
#endif
28+
}
29+
30+
public void Main(string[] args)
31+
{
32+
_logger.LogInformation("Starting");
33+
34+
var startTime = DateTimeOffset.UtcNow;
35+
_logger.LogInformation(1, "Started at '{StartTime}' and 0x{Hello:X} is hex of 42", startTime, 42);
36+
37+
try
38+
{
39+
throw new Exception("Boom");
40+
}
41+
catch (Exception ex)
42+
{
43+
_logger.LogCritical("Unexpected critical error starting application", ex);
44+
_logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null);
45+
// This write should not log anything
46+
_logger.Log(LogLevel.Critical, 0, null, null, null);
47+
_logger.LogError("Unexpected error", ex);
48+
_logger.LogWarning("Unexpected warning", ex);
49+
}
50+
51+
using (_logger.BeginScope("Main"))
52+
{
53+
Console.WriteLine("Hello World");
54+
55+
_logger.LogInformation("Waiting for user input");
56+
Console.ReadLine();
57+
}
58+
59+
var endTime = DateTimeOffset.UtcNow;
60+
_logger.LogInformation(2, "Stopping at '{StopTime}'", endTime);
61+
62+
_logger.LogInformation("Stopping");
63+
64+
_logger.LogInformation(Environment.NewLine);
65+
_logger.LogInformation("{Result,-10}{StartTime,15}{EndTime,15}{Duration,15}", "RESULT", "START TIME", "END TIME", "DURATION(ms)");
66+
_logger.LogInformation("{Result,-10}{StartTime,15}{EndTime,15}{Duration,15}", "------", "----- ----", "--- ----", "------------");
67+
_logger.LogInformation("{Result,-10}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds);
68+
}
69+
}
70+
}

samples/Sample/Sample.xproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>65357fbc-9bc4-466d-b621-1c3a19bc2a78</ProjectGuid>
11+
<RootNamespace>Sample</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
14+
</PropertyGroup>
15+
16+
<PropertyGroup>
17+
<SchemaVersion>2.0</SchemaVersion>
18+
</PropertyGroup>
19+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
20+
</Project>

samples/Sample/project.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": "1.0.0-*",
3+
"description": "",
4+
"authors": [ "" ],
5+
"tags": [ "" ],
6+
"projectUrl": "",
7+
"licenseUrl": "",
8+
9+
"dependencies": {
10+
"Microsoft.Framework.Logging": "1.0.0-*",
11+
"Serilog.Framework.Logging": "1.0.0-*",
12+
"Serilog.Sinks.Literate": "1.0.6"
13+
},
14+
15+
"commands": {
16+
"Sample": "Sample"
17+
},
18+
19+
"frameworks": {
20+
"dnx451": {
21+
"Serilog.Framework.Logging": "1.0.0-*"
22+
},
23+
"dnxcore50": {
24+
"dependencies": {
25+
"System.Console": "4.0.0-beta-22816",
26+
"System.Collections": "4.0.10-beta-22816",
27+
"System.Linq": "4.0.0-beta-22816",
28+
"System.Threading": "4.0.10-beta-22816",
29+
"Microsoft.CSharp": "4.0.0-beta-22816"
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)