Skip to content

Commit

Permalink
Merge pull request #3 from ghost1face/package
Browse files Browse the repository at this point in the history
Package
  • Loading branch information
ghost1face authored Nov 2, 2022
2 parents 2328a44 + 83e1ad1 commit df5963d
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 25 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/dotnet-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: package

on:
release:
types: [published]

jobs:
deploy:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- run: dotnet build --configuration Release .
- name: Publish the package to nuget.org
run: dotnet nuget push src/BeyondTech.Extensions.Logging.Timing/bin/Release/*.nupkg -k $NUGET_AUTH_TOKEN -s https://api.nuget.org/v3/index.json --skip-duplicate
env:
NUGET_AUTH_TOKEN: ${{secrets.NUGET_TOKEN}}
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ jobs:
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=coverage /p:CoverletOutputFormat=lcov
run: dotnet test --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=coverage /p:CoverletOutputFormat=lcov /p:Threshold=80 /p:ThresholdType=line /p:ThresholdStat=average
- name: Publish coverage report
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: ./test/BeyondTech.Extensions.Logging.Timing.Tests/coverage.info
path-to-lcov: ./test/BeyondTech.Extensions.Logging.Timing.Tests/coverage.net6.0.info
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
**/[Ll]og/
**/.vs/
**/.vscode/
*.csproj.user
*.csproj.user
coverage.*.info
6 changes: 6 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
<Version>1.0.0</Version>
</PropertyGroup>
</Project>
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
# BeyondTech Logging Extensions

### Timing [![Coverage Status](https://coveralls.io/repos/github/ghost1face/beyondtech-extensions-logging/badge.svg?branch=master)](https://coveralls.io/github/ghost1face/beyondtech-extensions-logging?branch=master)
Extensions for Microsoft's `ILogger` for common use cases.

Timing extensions for logging [here](./src/BeyondTech.Extensions.Logging.Timing/README.md)
## BeyondTech.Extensions.Logging.Timing
[![.NET Actions Status](https://github.com/ghost1face/beyondtech-extensions-logging/workflows/.NET/badge.svg?branch=master)](https://github.com/ghost1face/beyondtech-extensions-logging/actions) [![Coverage Status](https://coveralls.io/repos/github/ghost1face/beyondtech-extensions-logging/badge.svg?branch=master)](https://coveralls.io/github/ghost1face/beyondtech-extensions-logging?branch=master)
[![Nuget](https://img.shields.io/nuget/v/BeyondTech.Extensions.Logging.Timing.svg)](https://www.nuget.org/packages/BeyondTech.Extensions.Logging.Timing)


Timing extensions for logging operations: [here](./src/BeyondTech.Extensions.Logging.Timing/README.md)

This lets you perfom simple logging for timed operations, while simplifying the boilerplate:

```cs
ILogger logger = // assign instance
using (var operation = logger.BeginOperation("Processing large file {FilePath}", filePath))
{
operation.Complete();
}
```

Yields:

```
info: Processing large file /d/test/image.png completed in 822.5 ms
```

### License

See the [license](LICENSE.md)
Licensed under [Apache 2.0](LICENSE.md)
10 changes: 10 additions & 0 deletions beyondtech-extensions-logging.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeyondTech.Extensions.Loggi
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApp", "test\TestApp\TestApp.csproj", "{4D9054AD-3C3A-4795-AD5E-1361F0D215FE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0A2A68B3-F3E5-4654-92CE-064E2015AD78}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
LICENSE.md = LICENSE.md
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks)</TargetFrameworks>
<Version>$(Version)</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
<Authors>ghost1face</Authors>
<Copyright>2022</Copyright>
<Description>Extends ILogger for support for timed operations</Description>
<PackageProjectUrl>https://github.com/ghost1face/beyondtech-extensions-logging</PackageProjectUrl>
<RepositoryUrl>https://github.com/ghost1face/beyondtech-extensions-logging</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>csharp;logging;ilogger;extensions;timing;metrics;operations;</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="$(Configuration) == 'Release'">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.SourceLink.Github" Version="1.1.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/BeyondTech.Extensions.Logging.Timing/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum Properties

private const string OutcomeCompleted = "completed";
private const string OutcomeAbandoned = "abandoned";
private static readonly long StopwatchToTimeSpanTicks = Stopwatch.Frequency / TimeSpan.TicksPerSecond;
private static readonly double StopwatchToTimeSpanTicks = (double)Stopwatch.Frequency / TimeSpan.TicksPerSecond;

private readonly ILogger _logger;
private readonly string _messageTemplate;
Expand Down Expand Up @@ -67,7 +67,7 @@ internal Operation(ILogger logger, string messageTemplate, object[] args,

private static long GetTimestamp()
{
return Stopwatch.GetTimestamp() / StopwatchToTimeSpanTicks;
return unchecked((long)(Stopwatch.GetTimestamp() / StopwatchToTimeSpanTicks));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/BeyondTech.Extensions.Logging.Timing/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Timing Extensions [![Coverage Status](https://coveralls.io/repos/github/ghost1face/beyondtech-extensions-logging/badge.svg?branch=master)](https://coveralls.io/github/ghost1face/beyondtech-extensions-logging?branch=master)
# Timing Extensions

This library is a direct port of @nblumhardt 's [Serilog Timing](https://github.com/nblumhardt/serilog-timings) for usage directly with Microsoft's `ILogger`. This library provides ***almost*** all the same benefits of the Serilog Timing, but expands the support for any implementation of `ILogger`.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0</TargetFrameworks>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -20,10 +24,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
59 changes: 56 additions & 3 deletions test/BeyondTech.Extensions.Logging.Timing.Tests/OperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using BeyondTech.Extensions.Logging.Timing.Tests.Utilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

namespace BeyondTech.Extensions.Logging.Timing.Tests
Expand Down Expand Up @@ -158,7 +159,7 @@ public void OperationAt_LogsSpecifiedLevelWhenAbandoned()
const string operationName = "Long Op!";

using (var logger = new TailLogger(logs))
using (var operation = logger.Logger.OperationAt(LogLevel.Debug, LogLevel.Critical, TimeSpan.FromMilliseconds(1)).Begin(operationName))
using (var operation = logger.Logger.OperationAt(LogLevel.Debug, LogLevel.Critical, TimeSpan.FromMilliseconds(1000)).Begin(operationName))
{
operation.Abandon();
}
Expand All @@ -175,7 +176,7 @@ public void OperationAt_LogsSpecifiedLevelWhenComplete()
const string operationName = "Long Op!";

using (var logger = new TailLogger(logs))
using (var operation = logger.Logger.OperationAt(LogLevel.Debug, LogLevel.Critical, TimeSpan.FromMilliseconds(1)).Begin(operationName))
using (var operation = logger.Logger.OperationAt(LogLevel.Debug, LogLevel.Critical, TimeSpan.FromMilliseconds(1000)).Begin(operationName))
{
operation.Complete();
}
Expand All @@ -192,14 +193,46 @@ public void OperationAt_LogsNothingIfLogLevelIsNotSpecified()
const string operationName = "Long Op!";

using (var logger = new TailLogger(logs, LogLevel.Critical))
using (var operation = logger.Logger.OperationAt(LogLevel.Debug, LogLevel.Debug, TimeSpan.FromMilliseconds(1)).Begin(operationName))
using (var operation = logger.Logger.OperationAt(LogLevel.Debug, LogLevel.Debug, TimeSpan.FromMilliseconds(1000)).Begin(operationName))
{
operation.Complete();
}

Assert.Empty(logs);
}

[Fact]
public void Operation_ThrowsWhenCompleteIsCalledWithANullTemplate()
{
const string operationName = "Long Op!";

var logger = NullLogger<string>.Instance;
using (var operation = logger.BeginOperation(operationName))
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => operation.Complete(null));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}
}

[Fact]
public void Operation_LogsMessageOnComplete()
{
var logs = new List<string>();

const string operationName = "Long Op!";

using (var logger = new TailLogger(logs))
using (var operation = logger.Logger.OperationAt(LogLevel.Debug, LogLevel.Critical, TimeSpan.FromMilliseconds(1000)).Begin(operationName))
{
operation.Complete("This is complete!");
}

Assert.Single(logs);
Assert.StartsWith("dbug:", logs[0]);
Assert.Contains("This is complete", logs[0]);
}

[Fact]
public void Operation_LogsExceptionWhenSet()
{
Expand Down Expand Up @@ -247,9 +280,29 @@ public void Operation_SetExceptionAndRethrow_Throws()
public void Operation_ThrowsWhenILoggerIsNull()
{
ILogger? logger = null;
#pragma warning disable CS8604 // Possible null reference argument.
Assert.Throws<ArgumentNullException>(() => logger.OperationAt(LogLevel.Information));
Assert.Throws<ArgumentNullException>(() => logger.BeginOperation(""));
Assert.Throws<ArgumentNullException>(() => logger.TimeOperation(""));
#pragma warning restore CS8604 // Possible null reference argument.
}

[Fact]
public void Operation_ThrowsWhenMessageTemplateIsNull()
{
#pragma warning disable CS8604 // Possible null reference argument.
string? messageTemplate = null;
Assert.Throws<ArgumentNullException>(() => NullLogger<string>.Instance.BeginOperation(messageTemplate));
#pragma warning restore CS8604 // Possible null reference argument.
}

[Fact]
public void Operation_ThrowsWhenArgsAreNull()
{
#pragma warning disable CS8604 // Possible null reference argument.
object[]? args = null;
Assert.Throws<ArgumentNullException>(() => NullLogger<string>.Instance.BeginOperation("This is a template", args));
#pragma warning restore CS8604 // Possible null reference argument.
}
}
}
16 changes: 9 additions & 7 deletions test/TestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using BeyondTech.Extensions.Logging.Timing;
using Microsoft.Extensions.Logging;

Expand All @@ -16,17 +15,20 @@

try
{
logger.LogInformation("Hello, world!");

using (var op = logger.BeginOperation("Processing data {Data}", 123))
using (logger.TimeOperation("Full operation"))
{
await System.Threading.Tasks.Task.Delay(400);
op.Complete("Data processed with status code: {StatusCode}", 200);
logger.LogInformation("Hello, world!");

using (var op = logger.BeginOperation("Processing data {Data}", 123))
{
await System.Threading.Tasks.Task.Delay(400);
op.Complete("Data processed with status code: {StatusCode}", 200);
}
}

return 0;
}
catch (Exception ex)
catch
{
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions test/TestApp/TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit df5963d

Please sign in to comment.