Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Cross platform #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/SizeRollingExample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Serilog;
using Serilog.Sinks.RollingFileAlternate.Sinks.SizeRollingFileSink;

namespace SizeRollingExample
{
Expand Down
9 changes: 8 additions & 1 deletion serilog-sinks-rollingfile.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
VisualStudioVersion = 12.0.40629.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{9F725242-157F-444F-8029-8BAE22D3F78F}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{11
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SizeRollingExample", "examples\SizeRollingExample\SizeRollingExample.csproj", "{C78FDAA8-2135-41A1-84E1-63E8FB640C6B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Sinks.RollingFileAlternate.Portable", "src\Serilog.Sinks.RollingFileAlternate.Portable\Serilog.Sinks.RollingFileAlternate.Portable.csproj", "{D0129A92-AD43-4CE7-9090-4A5DF073995B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +41,10 @@ Global
{C78FDAA8-2135-41A1-84E1-63E8FB640C6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C78FDAA8-2135-41A1-84E1-63E8FB640C6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C78FDAA8-2135-41A1-84E1-63E8FB640C6B}.Release|Any CPU.Build.0 = Release|Any CPU
{D0129A92-AD43-4CE7-9090-4A5DF073995B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0129A92-AD43-4CE7-9090-4A5DF073995B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0129A92-AD43-4CE7-9090-4A5DF073995B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0129A92-AD43-4CE7-9090-4A5DF073995B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -47,5 +53,6 @@ Global
{D6DC593E-0CE9-423B-9FA8-33F2DF77A2EF} = {3E6972F1-7310-4BBF-A220-6B177FF4E65C}
{35BA7CB4-7E30-4E21-B176-B767295E5685} = {7647A4BD-398B-4497-B740-D0E128381C37}
{C78FDAA8-2135-41A1-84E1-63E8FB640C6B} = {11FB22BB-F1D5-47D4-AA2F-8EBD0103FF25}
{D0129A92-AD43-4CE7-9090-4A5DF073995B} = {3E6972F1-7310-4BBF-A220-6B177FF4E65C}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions src/Serilog.Sinks.RollingFileAlternate.Portable/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Serilog.Sinks.RollingFileAlternate
{
/// <summary>
/// Commonly used Constants.
/// </summary>
public class Constants
{

/// <summary>
/// The default output template used for log messages.
/// </summary>
public const string DefaultOutputTemplate =
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";

/// <summary>
/// The size of 2 megebytes in bytes.
/// </summary>
public const long TwoMegabytes = 1024 * 1024 * 2;

}
}
37 changes: 37 additions & 0 deletions src/Serilog.Sinks.RollingFileAlternate.Portable/IFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.IO;

namespace Serilog.Sinks.RollingFileAlternate
{
/// <summary>
/// Provides file system access.
/// </summary>
public interface IFileSystem
{
/// <summary>
/// Returns whether the specified directory exists.
/// </summary>
/// <returns></returns>
bool DirectoryExists(string path);

/// <summary>
/// Creates the specified directory.
/// </summary>
/// <param name="path"></param>
void CreateDirectory(string path);

/// <summary>
/// Opens the specified file in the mode necessary to append information only.
/// </summary>
/// <param name="fullPath"></param>
/// <returns></returns>
Stream OpenFileForAppend(string fullPath);

/// <summary>
/// Returns the names of the files in the specified directory.
/// </summary>
/// <param name="directory"></param>
/// <returns></returns>
IEnumerable<string> GetFiles(string directory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Formatting.Display;
using Serilog.Sinks.RollingFileAlternate.Sinks.HourlyRolling;
using Serilog.Sinks.RollingFileAlternate.Sinks.SizeRollingFileSink;

namespace Serilog.Sinks.RollingFileAlternate
{
/// <summary>
/// Configuration extensions to be able to use fluent syntax for constructing
/// a file sink that rolls files based on size.
/// </summary>
public static class LoggerConfigurationExtensions
{

/// <summary>
/// Creates an alternative implementation of the rolling file sink
/// that rolls files based on their size.
/// </summary>
/// <param name="configuration"><see cref="LoggerSinkConfiguration"/></param>
/// <param name="fileSystem">Provides access to the file system.</param>
/// <param name="logDirectory">The names of the directory to be logged</param>
/// <param name="minimumLevel">Minimum <see cref="LogEventLevel"/></param>
/// <param name="outputTemplate">The template for substituting logged parameters</param>
/// <param name="formatProvider">A culture specific format provider</param>
/// <param name="fileSizeLimitBytes">The size files should grow up to (default 2MB)</param>
/// <returns></returns>
public static LoggerConfiguration RollingFileAlternate(
this LoggerSinkConfiguration configuration,
IFileSystem fileSystem,
string logDirectory,
LogEventLevel minimumLevel = LevelAlias.Minimum,
string outputTemplate = Constants.DefaultOutputTemplate,
IFormatProvider formatProvider = null,
long? fileSizeLimitBytes = null)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}

var templateFormatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
var sink = new AlternateRollingFileSink(logDirectory, templateFormatter, fileSizeLimitBytes ?? Constants.TwoMegabytes, fileSystem);
return configuration.Sink(sink, minimumLevel);
}

/// <summary>
/// Creates an hourly rolling file sink that rolls files every hour.
/// </summary>
/// <param name="configuration"><see cref="LoggerSinkConfiguration"/></param>
/// <param name="fileSystem">Provides access to the file system.</param>
/// <param name="logDirectory">The names of the directory to be logged</param>
/// <param name="minimumLevel">Minimum <see cref="LogEventLevel"/></param>
/// <param name="outputTemplate">The template for substituting logged parameters</param>
/// <param name="formatProvider">A culture specific format provider</param>
/// <returns></returns>
public static LoggerConfiguration HourlyRollingFileAlternate(
this LoggerSinkConfiguration configuration,
IFileSystem fileSystem,
string logDirectory,
LogEventLevel minimumLevel = LevelAlias.Minimum,
string outputTemplate = Constants.DefaultOutputTemplate,
IFormatProvider formatProvider = null)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}

var templateFormatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
var sink = new HourlyRollingFileSink(logDirectory, templateFormatter, fileSystem);
return configuration.Sink(sink, minimumLevel);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("Serilog.Sinks.RollingFileAlternate.Portable")]
[assembly: AssemblyDescription("Serilog sink for RollingFile")]
[assembly: AssemblyCopyright("Copyright © Serilog Contributors 2014")]

[assembly: InternalsVisibleTo("Serilog.Sinks.RollingFileAlternate.Tests," +
"PublicKey=0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
"94191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066" +
"b19485ec")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D0129A92-AD43-4CE7-9090-4A5DF073995B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Serilog.Sinks.RollingFileAlternate</RootNamespace>
<AssemblyName>Serilog.Sinks.RollingFileAlternate.Portable</AssemblyName>
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DocumentationFile>bin\Debug\Serilog.Sinks.RollingFileAlternate.Portable.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>..\..\assets\Serilog.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Serilog, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Serilog.1.5.7\lib\net45\Serilog.dll</HintPath>
</Reference>
<Reference Include="Serilog.FullNetFx, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Serilog.1.5.7\lib\net45\Serilog.FullNetFx.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Constants.cs" />
<Compile Include="IFileSystem.cs" />
<Compile Include="LoggerConfigurationExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sinks\HourlyRolling\HourlyRollingFileSink.cs" />
<Compile Include="Sinks\HourlyRolling\HourlyFileSink.cs" />
<Compile Include="Sinks\HourlyRolling\HourlyLogFileDescription.cs" />
<Compile Include="Sinks\SizeRollingFileSink\LogFileInfo.cs" />
<Compile Include="Sinks\SizeRollingFileSink\SizeLimitedFileSink.cs" />
<Compile Include="Sinks\SizeRollingFileSink\SizeLimitedLogFileDescription.cs" />
<Compile Include="Sinks\SizeRollingFileSink\AlternateRollingFileSink.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace Serilog.Sinks.RollingFileAlternate.Sinks.HourlyRolling
{
using System;
using System.IO;
using System.Text;

using Serilog.Events;
using Serilog.Formatting;
using System;
using System.IO;
using System.Text;
using Serilog.Events;
using Serilog.Formatting;

namespace Serilog.Sinks.RollingFileAlternate.Sinks.HourlyRolling
{
internal class HourlyFileSink : IDisposable
{
private static readonly string ThisObjectName = typeof(HourlyFileSink).Name;
Expand All @@ -16,16 +15,18 @@ internal class HourlyFileSink : IDisposable
private readonly StreamWriter output;
private readonly object syncRoot = new object();
private bool disposed;
private IFileSystem fileSystem;

internal HourlyFileSink(
ITextFormatter formatter,
string logRootDirectory,
HourlyLogFileDescription hourlyLogFileDescription,
IFileSystem fileSytem,
Encoding encoding = null)
{
this.formatter = formatter;
this.hourlyLogFileDescription = hourlyLogFileDescription;

this.fileSystem = fileSytem;
string logDir = Path.Combine(logRootDirectory, hourlyLogFileDescription.Date.ToString("yyyy-MM-dd"));

this.output = this.OpenFileForWriting(logDir, hourlyLogFileDescription, encoding ?? Encoding.UTF8);
Expand Down Expand Up @@ -62,7 +63,7 @@ internal void Emit(LogEvent logEvent)

if (this.output == null)
{
return;
return;
}

this.formatter.Format(logEvent, this.output);
Expand All @@ -78,16 +79,15 @@ private StreamWriter OpenFileForWriting(
EnsureDirectoryCreated(folderPath);

var fullPath = Path.Combine(folderPath, logFileDescription.FileName);
var stream = File.Open(fullPath, FileMode.Append, FileAccess.Write, FileShare.Read);

var stream = fileSystem.OpenFileForAppend(fullPath);
return new StreamWriter(stream, encoding ?? Encoding.UTF8);
}

private static void EnsureDirectoryCreated(string path)
private void EnsureDirectoryCreated(string path)
{
if (!Directory.Exists(path))
if (!fileSystem.DirectoryExists(path))
{
Directory.CreateDirectory(path);
fileSystem.CreateDirectory(path);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Serilog.Sinks.RollingFileAlternate.Sinks.HourlyRolling
{
using System;
using System;

namespace Serilog.Sinks.RollingFileAlternate.Sinks.HourlyRolling
{
internal class HourlyLogFileDescription
{
private readonly DateTime dateTime;
Expand Down
Loading