Compare commits

..

9 commits

Author SHA1 Message Date
Ell
66842eec40 1.3.3 2023-11-30 22:58:11 +01:00
Ell
fff9f16068 allow writing to Console.Error using ConsoleSink 2023-11-30 22:57:43 +01:00
Ell
b9b6872949 1.3.2 2023-11-30 22:46:47 +01:00
Ell
d39abe603d added LogWriter class 2023-11-30 22:46:16 +01:00
Ell
56b0240d71 old command 2023-11-30 22:07:59 +01:00
Ell
6d55f25050 actually supply the key 2023-11-30 22:06:14 +01:00
Ell
7cab479901 oop 2023-11-30 22:05:18 +01:00
Ell
8c93c6f451 update to .net 8 2023-11-30 22:02:04 +01:00
Ell
d0972506b5 added wp buildscript 2023-11-30 21:57:34 +01:00
7 changed files with 124 additions and 38 deletions

19
.woodpecker/main.yml Normal file
View file

@ -0,0 +1,19 @@
steps:
build:
image: mcr.microsoft.com/dotnet/sdk:8.0
commands:
- dotnet build **/ExtremelySimpleLogger.csproj
pack:
image: mcr.microsoft.com/dotnet/sdk:8.0
commands:
- find . -type f -name '*.nupkg' -delete
- dotnet pack **/ExtremelySimpleLogger.csproj --version-suffix ci.$CI_PIPELINE_NUMBER
push:
when:
- event: [push, manual]
branch: main
image: mcr.microsoft.com/dotnet/sdk:8.0
commands:
- dotnet nuget push -s https://nuget.ellpeck.de/v3/index.json **/*.nupkg -k $BAGET_KEY -n
secrets:
- baget_key

View file

@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace ExtremelySimpleLogger {
/// <summary>
/// A <see cref="Sink"/> that writes log output to <see cref="Console.Out"/>.
/// A <see cref="Sink"/> that writes log output to <see cref="Console.Out"/> or <see cref="Console.Error"/>.
/// </summary>
public class ConsoleSink : Sink {
@ -17,6 +18,15 @@ namespace ExtremelySimpleLogger {
{LogLevel.Fatal, ConsoleColor.DarkRed}
};
private readonly object locker = new object();
private readonly TextWriter console;
/// <summary>
/// Creates a new console sink with the given settings.
/// </summary>
/// <param name="error">Whether to log to <see cref="Console.Error"/> instead of <see cref="Console.Out"/>.</param>
public ConsoleSink(bool error = false) {
this.console = error ? Console.Error : Console.Out;
}
/// <summary>
/// Sets the <see cref="ConsoleColor"/> that text with the given <see cref="LogLevel"/> should be displayed with.
@ -57,11 +67,11 @@ namespace ExtremelySimpleLogger {
var color = this.GetColor(level);
if (color.HasValue)
Console.ForegroundColor = color.Value;
Console.WriteLine(s);
this.console.WriteLine(s);
if (color.HasValue)
Console.ResetColor();
}
}
}
}
}

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>net45;netstandard2.0;net8.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
@ -14,7 +14,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Logo.png</PackageIcon>
<VersionPrefix>1.3.1</VersionPrefix>
<VersionPrefix>1.3.3</VersionPrefix>
</PropertyGroup>
<ItemGroup>

View file

@ -0,0 +1,82 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace ExtremelySimpleLogger {
/// <summary>
/// Implementation of a <see cref="TextWriter"/> that writes to a <see cref="Logger"/>.
/// </summary>
public class LogWriter : TextWriter {
/// <summary>
/// The log level that this log writer should write with.
/// </summary>
public LogLevel Level {
get {
lock (this.logger)
return this.level;
}
set {
lock (this.logger)
this.level = value;
}
}
/// <inheritdoc />
public override Encoding Encoding => Encoding.UTF8;
private readonly StringBuilder line = new StringBuilder();
private readonly Logger logger;
private LogLevel level;
/// <summary>
/// Creates a new log writer with the given settings.
/// </summary>
/// <param name="logger">The logger to write to.</param>
/// <param name="level">The log level to write with.</param>
public LogWriter(Logger logger, LogLevel level = LogLevel.Info) {
this.logger = logger;
this.level = level;
}
/// <inheritdoc />
public override void Write(char value) {
lock (this.logger)
this.line.Append(value);
}
/// <inheritdoc />
public override void Write(char[] buffer, int index, int count) {
lock (this.logger)
this.line.Append(buffer, index, count);
}
/// <inheritdoc />
public override void Write(string value) {
lock (this.logger)
this.line.Append(value);
}
/// <inheritdoc />
public override void WriteLine(string value) {
this.Write(value);
this.WriteLine();
}
/// <inheritdoc />
public override void WriteLine() {
this.Flush();
}
/// <inheritdoc />
public override void Flush() {
lock (this.logger) {
this.logger.Log(this.level, this.line);
this.line.Clear();
}
}
}
}

31
Jenkinsfile vendored
View file

@ -1,31 +0,0 @@
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'dotnet build **/ExtremelySimpleLogger.csproj'
}
}
stage('Pack') {
steps {
sh 'find . -type f -name \\\'*.nupkg\\\' -delete'
sh 'dotnet pack **/ExtremelySimpleLogger.csproj --version-suffix ${BUILD_NUMBER}'
}
}
stage('Publish') {
when {
branch 'main'
}
steps {
sh '''dotnet nuget push -s http://localhost:5000/v3/index.json **/*.nupkg -k $BAGET -n true
'''
}
}
}
environment {
BAGET = credentials('3db850d0-e6b5-43d5-b607-d180f4eab676')
}
}

View file

@ -39,9 +39,15 @@ namespace Sample {
logger.Log(LogLevel.Trace, "This is a message that only the file sink will receive, since its minimum level is lower.");
logger.Log(LogLevel.Info, "The program finished.");
// we can also use a writer to write to the log
Console.SetError(new LogWriter(logger, LogLevel.Warn));
Console.Error.WriteLine("This is an error written through serr! Oh no!");
Console.Error.Write("This is another error, but ");
Console.Error.WriteLine("written in multiple parts!");
// Once we're done using the logger, we can dispose it so that our FileSink instances free their files
logger.Dispose();
}
}
}
}

View file

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>