Skip to content

Commit

Permalink
Initial cut of console sink as an example of porting sinks
Browse files Browse the repository at this point in the history
  • Loading branch information
merbla committed Jan 12, 2016
1 parent 0b7aba9 commit e0093bc
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 28 deletions.
10 changes: 10 additions & 0 deletions Serilog.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog", "src\Serilog\Seri
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Tests", "test\Serilog.Tests\Serilog.Tests.xproj", "{3C2D8E01-5580-426A-BDD9-EC59CD98E618}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Sinks.Console", "src\Serilog.Sinks.Console\Serilog.Sinks.Console.xproj", "{866A028E-27DB-49A0-AC78-E5FEF247C099}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sinks", "sinks", "{9EC69873-5A97-4C25-AB5A-31DDE589B2D9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,12 +39,18 @@ Global
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.Build.0 = Release|Any CPU
{866A028E-27DB-49A0-AC78-E5FEF247C099}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{866A028E-27DB-49A0-AC78-E5FEF247C099}.Debug|Any CPU.Build.0 = Debug|Any CPU
{866A028E-27DB-49A0-AC78-E5FEF247C099}.Release|Any CPU.ActiveCfg = Release|Any CPU
{866A028E-27DB-49A0-AC78-E5FEF247C099}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{803CD13A-D54B-4CEC-A55F-E22AE3D93B3C} = {037440DE-440B-4129-9F7A-09B42D00397E}
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {0D135C0C-A60B-454A-A2F4-CD74A30E04B0}
{866A028E-27DB-49A0-AC78-E5FEF247C099} = {9EC69873-5A97-4C25-AB5A-31DDE589B2D9}
{9EC69873-5A97-4C25-AB5A-31DDE589B2D9} = {037440DE-440B-4129-9F7A-09B42D00397E}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Serilog.Sinks.SystemConsole
{
class ConsoleSink : ILogEventSink
public class ConsoleSink : ILogEventSink
{
readonly ITextFormatter _textFormatter;

Expand Down
39 changes: 39 additions & 0 deletions src/Serilog.Sinks.Console/LoggingExtentsions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting.Display;


namespace Serilog.Sinks.SystemConsole
{
public static class LoggingExtentsions
{
const string DefaultConsoleOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}";

/// <summary>
/// Writes log events to <see cref="System.Console"/>.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration Console(
this LoggerSinkConfiguration sinkConfiguration,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = DefaultConsoleOutputTemplate,
IFormatProvider formatProvider = null,
LoggingLevelSwitch levelSwitch = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return sinkConfiguration.Sink(new ConsoleSink(formatter), restrictedToMinimumLevel, levelSwitch);
}
}
}
20 changes: 20 additions & 0 deletions src/Serilog.Sinks.Console/Serilog.Sinks.Console.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>866a028e-27db-49a0-ac78-e5fef247c099</ProjectGuid>
<RootNamespace>Serilog.Sinks.Console</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>
60 changes: 60 additions & 0 deletions src/Serilog.Sinks.Console/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"version": "2.0.0-beta-*",
"description": "The console sink for Seriolog - Simple .NET logging with fully-structured events",
"authors": [ "Serilog Contributors" ],
"tags": [ "serilog", "logging", "semantic", "structured", "console" ],
"projectUrl": "http://serilog.net",
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0",
"iconUrl": "http://serilog.net/images/serilog-nuget.png",
"frameworks": {
"net40": {
"compilationOptions": {
"keyFile": "../../assets/Serilog.snk"
},
"frameworkAssemblies": {
"System.Configuration": ""
},
"dependencies": {
"Microsoft.Bcl.Async": "1.0.168",
"Serilog": "2.0.0-beta-*"
}
},
"net45": {
"compilationOptions": {
"keyFile": "../../assets/Serilog.snk"
},
"frameworkAssemblies": {
"System.Configuration": ""
},
"dependencies": {
"Serilog": "2.0.0-beta-*"
}
},
"dnx451": {
"compilationOptions": {
"keyFile": "../../assets/Serilog.snk"
},
"frameworkAssemblies": {
"System.Configuration": ""
},
"dependencies": {
"Serilog": "2.0.0-beta-*"
}
},
"dotnet5.4": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"Serilog": "2.0.0-beta-*",
"System.Collections.Concurrent": "4.0.11-beta-23516",
"System.Console": "4.0.0-beta-23516",
"System.Diagnostics.Process": "4.1.0-beta-23516",
"System.Diagnostics.TraceSource": "4.0.0-beta-23516",
"System.IO": "4.0.11-beta-23516",
"System.IO.FileSystem": "4.0.1-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Text.RegularExpressions": "4.0.11-beta-23516",
"System.Threading.Thread": "4.0.0-beta-23516"
}
}
}
}
26 changes: 1 addition & 25 deletions src/Serilog/LoggerConfigurationFullNetFxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,6 @@ public static class LoggerConfigurationFullNetFxExtensions
const long DefaultFileSizeLimitBytes = 1L * 1024 * 1024 * 1024;
const int DefaultRetainedFileCountLimit = 31; // A long month of logs

/// <summary>
/// Writes log events to <see cref="System.Console"/>.
/// </summary>
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration Console(
this LoggerSinkConfiguration sinkConfiguration,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = DefaultConsoleOutputTemplate,
IFormatProvider formatProvider = null,
LoggingLevelSwitch levelSwitch = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return sinkConfiguration.Sink(new ConsoleSink(formatter), restrictedToMinimumLevel, levelSwitch);
}

/// <summary>
/// Writes log events to <see cref="System.Console"/>, using color to differentiate
Expand Down Expand Up @@ -322,7 +298,7 @@ public static LoggerConfiguration AppSettings(
{
if (settingPrefix.Contains(":")) throw new ArgumentException("Custom setting prefixes cannot contain the colon (:) character.");
if (settingPrefix == "serilog") throw new ArgumentException("The value \"serilog\" is not a permitted setting prefix. To use the default, do not specify a custom prefix at all.");
if (string.IsNullOrWhiteSpace(settingPrefix)) throw new ArgumentException("To use the default setting prefix, do not supply the settingPrefix parameter, instead pass the default null.");
if (String.IsNullOrWhiteSpace(settingPrefix)) throw new ArgumentException("To use the default setting prefix, do not supply the settingPrefix parameter, instead pass the default null.");
}

return settingConfiguration.Settings(new AppSettingsSettings(settingPrefix));
Expand Down
5 changes: 3 additions & 2 deletions test/Serilog.Tests/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"version": "2.0.0-beta-*",
"commands": {
"test": "xunit.runner.dnx",
Expand All @@ -10,7 +10,8 @@
"Serilog": { "target": "project" },
"xunit": "2.1.0",
"xunit.runner.visualstudio": "2.1.0",
"xunit.runner.dnx": "2.1.0-rc1-build204"
"xunit.runner.dnx": "2.1.0-rc1-build204",
"Serilog.Sinks.Console": "2.0.0-beta-*"
},
"frameworks": {
"dnx451": {
Expand Down

0 comments on commit e0093bc

Please sign in to comment.