Skip to content

Commit

Permalink
Merge pull request #39 from jjm-one/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jonas-merkle authored Nov 2, 2023
2 parents 9927284 + 1d2d69c commit a814e6c
Show file tree
Hide file tree
Showing 17 changed files with 1,990 additions and 1,805 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,30 @@ jobs:
needs: build-and-test-release
runs-on: windows-latest
steps:
- name: Set up JDK 11
uses: actions/setup-java@v1
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 1.11
java-version: 17
distribution: 'zulu' # Alternative distribution options are available.
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Cache SonarCloud packages
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ~\sonar\cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache SonarCloud scanner
id: cache-sonar-scanner
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: .\.sonar\scanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner
- name: Cache dotnet-coverage
id: cache-dotnet-coverage
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: .\.dotnet-coverage\dotnet-coverage
key: ${{ runner.os }}-dotnet-coverage
Expand Down
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@ A basic Slack Sink for the Serilog framework.

## Status

| | |
|----------------------:|-----------------------|
| Build & Test Status (main) | [![Build&Test](https://github.com/jjm-one/jjm.one.Serilog.Sinks.SlackWebHook/actions/workflows/dotnet.yml/badge.svg)](https://github.com/jjm-one/jjm.one.Serilog.Sinks.SlackWebHook/actions/workflows/dotnet.yml) |
| Nuget Package Version | [![Nuget Version](https://img.shields.io/nuget/v/jjm.one.Serilog.Sinks.SlackWebHook?style=flat-square)](https://www.nuget.org/packages/jjm.one.Serilog.Sinks.SlackWebHook/) |
| | |
|------------------------------:|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Build & Test Status (main) | [![Build&Test](https://github.com/jjm-one/jjm.one.Serilog.Sinks.SlackWebHook/actions/workflows/dotnet.yml/badge.svg)](https://github.com/jjm-one/jjm.one.Serilog.Sinks.SlackWebHook/actions/workflows/dotnet.yml) |
| Nuget Package Version | [![Nuget Version](https://img.shields.io/nuget/v/jjm.one.Serilog.Sinks.SlackWebHook?style=flat-square)](https://www.nuget.org/packages/jjm.one.Serilog.Sinks.SlackWebHook/) |
| SonarCloudQuality Gate Status | [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jjm-one_jjm.one.Serilog.Sinks.SlackWebHook&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=jjm-one_jjm.one.Serilog.Sinks.SlackWebHook) |

## Description

This tool provides a Serilog Sink which sends log messages to one or more channels in a Slack workspace. To send this messages this tool is using the [Slack.Webhooks](https://github.com/mrb0nj/Slack.Webhooks) project to handle the communication with Slack. Therefor you need a valid WebHook URL to use this Sink. To get such a WebHook URL please read this [article](https://slack.com/help/articles/115005265063-Incoming-Webhooks-for-Slack). The log messages will be send in batches to the slack servers. You can setup the batch size to fit your needs.
This tool provides a Serilog Sink which sends log messages to one or more channels in a Slack workspace. To send this
messages this tool is using the [Slack.Webhooks](https://github.com/mrb0nj/Slack.Webhooks) project to handle the
communication with Slack. Therefor you need a valid WebHook URL to use this Sink. To get such a WebHook URL please read
this [article](https://slack.com/help/articles/115005265063-Incoming-Webhooks-for-Slack). The log messages will be send
in batches to the slack servers. You can setup the batch size to fit your needs.

## Nuget Package

You can get the latest version of this software as a nuget package form [nuget.org](https://www.nuget.org/packages/jjm.one.Serilog.Sinks.SlackWebHook/)
You can get the latest version of this software as a nuget package
form [nuget.org](https://www.nuget.org/packages/jjm.one.Serilog.Sinks.SlackWebHook/)

### Installing the Nuget Package

| Tool | Command/Code |
|----------------------|--------------|
| Package Manager | ```PM> Install-Package jjm.one.Serilog.Sinks.SlackWebHook -Version X.Y.Z``` |
| .NET CLI | ```> dotnet add package jjm.one.Serilog.Sinks.SlackWebHook --version X.Y.Z``` |
| Tool | Command/Code |
|----------------------|-----------------------------------------------------------------------------------------|
| Package Manager | ```PM> Install-Package jjm.one.Serilog.Sinks.SlackWebHook -Version X.Y.Z``` |
| .NET CLI | ```> dotnet add package jjm.one.Serilog.Sinks.SlackWebHook --version X.Y.Z``` |
| PackageReference | ```<PackageReference Include="jjm.one.Serilog.Sinks.SlackWebHook" Version="X.Y.Z" />``` |
| Package CLI | ```> paket add jjm.one.Serilog.Sinks.SlackWebHook --version X.Y.Z``` |
| Script & Interactive | ```> #r "nuget: jjm.one.Serilog.Sinks.SlackWebHook, X.Y.Z"``` |
| Cake as Addin | ```#addin nuget:?package=jjm.one.Serilog.Sinks.SlackWebHook&version=X.Y.Z``` |
| Cake as Tool | ```#tool nuget:?package=jjm.one.Serilog.Sinks.SlackWebHook&version=X.Y.Z``` |
| Package CLI | ```> paket add jjm.one.Serilog.Sinks.SlackWebHook --version X.Y.Z``` |
| Script & Interactive | ```> #r "nuget: jjm.one.Serilog.Sinks.SlackWebHook, X.Y.Z"``` |
| Cake as Addin | ```#addin nuget:?package=jjm.one.Serilog.Sinks.SlackWebHook&version=X.Y.Z``` |
| Cake as Tool | ```#tool nuget:?package=jjm.one.Serilog.Sinks.SlackWebHook&version=X.Y.Z``` |

## Dependencies

Expand Down Expand Up @@ -120,7 +125,8 @@ Log.Logger = new LoggerConfiguration()

## Full Documentation

The full documentation for this package can be found [here](https://jjm-one.github.io/jjm.one.Serilog.Sinks.SlackWebHook/main/doc/html/index.html).
The full documentation for this package can be
found [here](https://jjm-one.github.io/jjm.one.Serilog.Sinks.SlackWebHook/main/doc/html/index.html).

## Repo

Expand Down
169 changes: 89 additions & 80 deletions src/jjm.one.Serilog.Sinks.SlackWebHook.Example/SlackSinkExample.cs
Original file line number Diff line number Diff line change
@@ -1,88 +1,97 @@
using Serilog;
using System;
using System.Threading;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using System;

namespace jjm.one.Serilog.Sinks.SlackWebHook.Example
namespace jjm.one.Serilog.Sinks.SlackWebHook.Example;

/// <summary>
/// This class contains examples on how to use the library.
/// </summary>
public static class SlackSinkExample
{
public static class SlackSinkExample
/// <summary>
/// The main function of the <see cref="SlackSinkExample"/> class.
/// </summary>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="Exception"></exception>
public static void Main()
{
public static void Main()
string? slackChannel = null;

Console.WriteLine(
"####################################################################################################");
Console.WriteLine("Serilog Slack Sink Example (ver. " + typeof(SlackSinkExample).Assembly.GetName().Version +
")");
Console.WriteLine(
"####################################################################################################");
Console.WriteLine();

Console.Write("Slack WebHook Url: ");
var input = Console.ReadLine();
if (string.IsNullOrEmpty(input)) throw new ArgumentException("No valid WebHook Url!");
var slackWebHookUrl = input;
Console.WriteLine();

Console.Write("Slack Channel (optional): ");
input = Console.ReadLine();
if (!string.IsNullOrEmpty(input)) slackChannel = input;
Console.WriteLine();

Console.Write("Slack Username (optional): ");
input = Console.ReadLine();
var slackUsername = string.IsNullOrEmpty(input) ? input : "Serilog Slack Sink Example";
Console.WriteLine();

Console.Write("Slack User Icon (optional): ");
input = Console.ReadLine();
var slackUserIcon = string.IsNullOrEmpty(input) ? input : ":monkey_face:";
Console.WriteLine();

var levelSwitch = new LoggingLevelSwitch(LogEventLevel.Verbose);

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Slack(
slackWebHookUrl,
slackChannel,
slackUsername,
slackUserIcon,
periodicBatchingSinkOptionsBatchSizeLimit: 1,
periodicBatchingSinkOptionsPeriod: TimeSpan.FromMilliseconds(10),
periodicBatchingSinkOptionsQueueLimit: 100,
sinkRestrictedToMinimumLevel: LogEventLevel.Verbose,
sinkOutputTemplate: "{Message}",
sinkLevelSwitch: levelSwitch
)
.CreateLogger();

Log.Write(LogEventLevel.Verbose, "Verbose Logging Message");
Log.Write(LogEventLevel.Debug, "Debug Logging Message");
Log.Write(LogEventLevel.Information, "Information Logging Message");
Log.Write(LogEventLevel.Warning, "Warning Logging Message");
Log.Write(LogEventLevel.Error, "Error Logging Message");
Log.Write(LogEventLevel.Fatal, "Fatal Logging Message");

try
{
throw new Exception("TEST EXCEPTION!");
}
catch (Exception e)
{
string slackChannel = null;

Console.WriteLine("####################################################################################################");
Console.WriteLine("Serilog Slack Sink Example (ver. " + typeof(SlackSinkExample).Assembly.GetName().Version + ")");
Console.WriteLine("####################################################################################################");
Console.WriteLine();

Console.Write("Slack WebHook Url: ");
var input = Console.ReadLine();
if (string.IsNullOrEmpty(input)) throw new ArgumentException("No valid WebHook Url!");
var slackWebHookUrl = input;
Console.WriteLine();

Console.Write("Slack Channel (optional): ");
input = Console.ReadLine();
if (!string.IsNullOrEmpty(input)) slackChannel = input;
Console.WriteLine();

Console.Write("Slack Username (optional): ");
input = Console.ReadLine();
var slackUsername = string.IsNullOrEmpty(input) ? input : "Serilog Slack Sink Example";
Console.WriteLine();

Console.Write("Slack User Icon (optional): ");
input = Console.ReadLine();
var slackUserIcon = string.IsNullOrEmpty(input) ? input : ":monkey_face:";
Console.WriteLine();

var levelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Verbose);

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Slack(
slackWebHookUrl: slackWebHookUrl,
slackChannel: slackChannel,
slackUsername: slackUsername,
slackEmojiIcon: slackUserIcon,

periodicBatchingSinkOptionsBatchSizeLimit: 1,
periodicBatchingSinkOptionsPeriod: TimeSpan.FromMilliseconds(10),
periodicBatchingSinkOptionsQueueLimit: 100,

sinkRestrictedToMinimumLevel: LogEventLevel.Verbose,
sinkOutputTemplate: "{Message}",
sinkLevelSwitch: levelSwitch
)
.CreateLogger();

Log.Write(LogEventLevel.Verbose, "Verbose Logging Message");
Log.Write(LogEventLevel.Debug, "Debug Logging Message");
Log.Write(LogEventLevel.Information, "Information Logging Message");
Log.Write(LogEventLevel.Warning, "Warning Logging Message");
Log.Write(LogEventLevel.Error, "Error Logging Message");
Log.Write(LogEventLevel.Fatal, "Fatal Logging Message");

try
{
throw new Exception("TEST EXCEPTION!");
}
catch (Exception e)
{
Log.Write(LogEventLevel.Fatal, e, "Fatal Logging Message with exception");
}

levelSwitch.MinimumLevel = LogEventLevel.Fatal;

Log.Write(LogEventLevel.Error, "This Message shouldn't be send to Slack!");

System.Threading.Thread.Sleep(10000);

Console.WriteLine("Press any key to finish...");
Console.ReadKey();

Environment.Exit(0);
Log.Write(LogEventLevel.Fatal, e, "Fatal Logging Message with exception");
}

levelSwitch.MinimumLevel = LogEventLevel.Fatal;

Log.Write(LogEventLevel.Error, "This Message shouldn't be send to Slack!");

Thread.Sleep(10000);

Console.WriteLine("Press any key to finish...");
Console.ReadKey();

Environment.Exit(0);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.2</Version>
<Authors>Jonas Merkle [JJM] </Authors>
<Copyright>© by Jonas Merkle [JJM], 2023.</Copyright>
<RootNamespace>jm.one.Serilog.Sinks.SlackWebHook.Example</RootNamespace>
<Title>jm.one.Serilog.Sinks.SlackWebHook.Example</Title>
<Description>A basic Slack Sink for the Serilog framwork. (Example)</Description>
<PackageTags>serilog, serilog-sink, slack, logging, csharp, example</PackageTags>
<PackageProjectUrl>https://github.com/jjm-one/jjm.one.Serilog.Sinks.SlackWebHook</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/jjm-one/jjm.one.Serilog.Sinks.SlackWebHook</RepositoryUrl>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.1.0</Version>
<Authors>Jonas Merkle [JJM]</Authors>
<Copyright>© by Jonas Merkle [JJM], 2023.</Copyright>
<Title>jjm.one.Serilog.Sinks.SlackWebHook.Example</Title>
<Description>A basic Slack Sink for the Serilog framwork. (Example)</Description>
<PackageTags>serilog, serilog-sink, slack, logging, csharp, example</PackageTags>
<PackageProjectUrl>https://github.com/jjm-one/jjm.one.Serilog.Sinks.SlackWebHook</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/jjm-one/jjm.one.Serilog.Sinks.SlackWebHook</RepositoryUrl>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<LangVersion>latestmajor</LangVersion>
<DocumentationFile>./jjm.one.MiscUtilFunctions.Tests.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<LangVersion>latestmajor</LangVersion>
<DocumentationFile>./jjm.one.MiscUtilFunctions.Tests.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<LangVersion>latestmajor</LangVersion>
<DocumentationFile>./jjm.one.MiscUtilFunctions.Tests.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<LangVersion>latestmajor</LangVersion>
<DocumentationFile>./jjm.one.MiscUtilFunctions.Tests.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="3.0.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="3.0.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\jjm.one.Serilog.Sinks.SlackWebHook\jjm.one.Serilog.Sinks.SlackWebHook.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\jjm.one.Serilog.Sinks.SlackWebHook\jjm.one.Serilog.Sinks.SlackWebHook.csproj"/>
</ItemGroup>

</Project>
Loading

0 comments on commit a814e6c

Please sign in to comment.