mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-27 11:58:34 +01:00
Compare commits
9 commits
4d5478bcf2
...
66842eec40
Author | SHA1 | Date | |
---|---|---|---|
66842eec40 | |||
fff9f16068 | |||
b9b6872949 | |||
d39abe603d | |||
56b0240d71 | |||
6d55f25050 | |||
7cab479901 | |||
8c93c6f451 | |||
d0972506b5 |
7 changed files with 124 additions and 38 deletions
19
.woodpecker/main.yml
Normal file
19
.woodpecker/main.yml
Normal 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
|
|
@ -1,9 +1,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace ExtremelySimpleLogger {
|
namespace ExtremelySimpleLogger {
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public class ConsoleSink : Sink {
|
public class ConsoleSink : Sink {
|
||||||
|
|
||||||
|
@ -17,6 +18,15 @@ namespace ExtremelySimpleLogger {
|
||||||
{LogLevel.Fatal, ConsoleColor.DarkRed}
|
{LogLevel.Fatal, ConsoleColor.DarkRed}
|
||||||
};
|
};
|
||||||
private readonly object locker = new object();
|
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>
|
/// <summary>
|
||||||
/// Sets the <see cref="ConsoleColor"/> that text with the given <see cref="LogLevel"/> should be displayed with.
|
/// Sets the <see cref="ConsoleColor"/> that text with the given <see cref="LogLevel"/> should be displayed with.
|
||||||
|
@ -57,7 +67,7 @@ namespace ExtremelySimpleLogger {
|
||||||
var color = this.GetColor(level);
|
var color = this.GetColor(level);
|
||||||
if (color.HasValue)
|
if (color.HasValue)
|
||||||
Console.ForegroundColor = color.Value;
|
Console.ForegroundColor = color.Value;
|
||||||
Console.WriteLine(s);
|
this.console.WriteLine(s);
|
||||||
if (color.HasValue)
|
if (color.HasValue)
|
||||||
Console.ResetColor();
|
Console.ResetColor();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net45;netstandard2.0;net6.0</TargetFrameworks>
|
<TargetFrameworks>net45;netstandard2.0;net8.0</TargetFrameworks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<PackageIcon>Logo.png</PackageIcon>
|
<PackageIcon>Logo.png</PackageIcon>
|
||||||
<VersionPrefix>1.3.1</VersionPrefix>
|
<VersionPrefix>1.3.3</VersionPrefix>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
82
ExtremelySimpleLogger/LogWriter.cs
Normal file
82
ExtremelySimpleLogger/LogWriter.cs
Normal 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
31
Jenkinsfile
vendored
|
@ -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')
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -39,6 +39,12 @@ 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.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.");
|
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
|
// Once we're done using the logger, we can dispose it so that our FileSink instances free their files
|
||||||
logger.Dispose();
|
logger.Dispose();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
Loading…
Reference in a new issue