Coder Social home page Coder Social logo

serilog-contrib / serilog.sinks.microsoftteams.alternative Goto Github PK

View Code? Open in Web Editor NEW
30.0 5.0 10.0 6.4 MB

Serilog.Sinks.MicrosoftTeams.Alternative is a library to save logging information from Serilog to Microsoft Teams.

License: MIT License

C# 98.63% Batchfile 1.37%
csharp serilog ms-teams microsoft-teams teams log logging

serilog.sinks.microsoftteams.alternative's Introduction

Serilog.Sinks.MicrosoftTeams.Alternative

Serilog.Sinks.MicrosoftTeams.Alternative is a library to save logging information from Serilog to Microsoft Teams.

GitHub issues GitHub forks GitHub stars License: MIT Nuget NuGet Downloads Known Vulnerabilities Gitter

All Contributors

Available for

  • Net 6.0
  • Net 8.0

Net Core and Net Framework latest and LTS versions

Basic usage

Check out the how to use file here.

Install

dotnet add package Serilog.Sinks.MicrosoftTeams.Alternative

Change history

See the Changelog.

Contributors โœจ

Thanks goes to these wonderful people (emoji key):

ruisantos
ruisantos

๐Ÿ’ป ๐Ÿ“–
Appelg
Appelg

๐Ÿ’ป ๐Ÿ“–
maestroMike
maestroMike

๐Ÿ’ป ๐Ÿ“–
HansM
HansM

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿšง ๐Ÿ“† โš ๏ธ
Serilog Contrib
Serilog Contrib

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿšง ๐Ÿ“† โš ๏ธ
Nelson Oliveira
Nelson Oliveira

๐Ÿ’ป ๐Ÿ“–
Goman
Goman

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

serilog.sinks.microsoftteams.alternative's People

Contributors

allcontributors[bot] avatar appelg avatar dependabot[bot] avatar mbardynin avatar nelsonroliveira avatar ruisantos avatar sepppenner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

serilog.sinks.microsoftteams.alternative's Issues

Filtered output and multiple channels

Support for filtering output events and multiple channels

A common usage case for me is logging only certain events to teams, i do this by adding a "logger" sink with a filter expression to search for a specific property.

Recently i had a request to log to multiple channels, this could be done by adding several more sinks with the different filters and url, but this would make the configuration file extremely verbose.

Would this project be willing to accept a PR to add:

  1. Option to filter and send only events containing a specific property
  2. Option to set a dictionary for the url to be used for the value found in the filter property (multiple channels support)

Proposed workings

  1. The user of the sink sets it to filter on events that have a property "MsTeams" (for example)
  2. before logging an event the user adds the property "MsTeams" with whatever value represents the intended channel
  3. During the push of the events
    • if the "MsTeams" property exists, it's value is searched as a key in the url dictionary to get the targeted url
    • if the value for the "MsTeams" is not found in the url dictionary, use the default url for the sink
    • if there is no property with that name, discard the event

Proposed configuration json

        "WriteTo": [
        {
            "Name": "MicrosoftTeams",
            "Args": {
                "webHookUri": "https://example.com/defaultChannel",
                "channelHandler": {
                    "filterOnProperty": "MsTeams",
                    "channelList": {
                        "MainChannel": "https://example.com/MainChannel",
                        "ITChannel": "https://example.com/ITChannel"
                    }
                }
            }
        }
    ]

Final thoughts

This is still a rough idea of what would be needed, but i would like to contribute this to the community in a way that would be useful for everyone.

The version 1.4.0 doesn't work with .NET 6.0.

The version 1.3.0 works well.

The dll in the package directory ( serilog.sinks.microsoftteams.alternative\1.4.0\lib\net6.0 ) targets .NET 7.0.

Here is the exception:
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'

Formatting improvement

I'm logging an exception (stack trace) to Teams and it could use some newlines for readability. There are also some backticks in my stack trace that leads to Teams thinking it should render the section between as code (monospace font with a background). Sounds like there needs to be some html escaping or conversion?

BatchSizeLimit > 1 causes the logger to crash

When setting BatchSizeLimit to a number > 1 the microsoft teams logger throws a null reference exception.

Log.Logger = new LoggerConfiguration().MinimumLevel.Information()
                              .WriteTo.MicrosoftTeams(webHookUri: myWebHookUri, title: "mytitle", batchSizeLimit: 100)                  
                              .CreateLogger();
Log.Logger.Information("test");

Self Log:
2020-07-13T08:38:01.3053211Z Exception while emitting periodic batch from Serilog.Sinks.MicrosoftTeams.MicrosoftTeamsSink: System.NullReferenceException: Object reference not set to an instance of an object.
at Serilog.Sinks.MicrosoftTeams.MicrosoftTeamsSink.<>c__DisplayClass4_0.b__0(ExtendedLogEvent l)
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Func2 predicate, Boolean& found)
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate)
at Serilog.Sinks.MicrosoftTeams.MicrosoftTeamsSink.EmitBatchAsync(IEnumerable`1 events)
at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.OnTick()

Add option for hiding properties section in message

In the current implementation, it is not possible to control how message will look like.
For example Properties section in most cases looks like noise in ms teams channel.
Would be great to have an option like omitPropertiesSection:

var logger = new LoggerConfiguration()
	.WriteTo.MicrosoftTeams(webHookUri, title: title, omitPropertiesSection: true)
    .CreateLogger();

Error with asp.net Core 2.2 app

After installing via NuGet, upon running the application (without setting any additional configuration), I received this error -

HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure
Common causes of this issue:
The application process failed to start
The application process started but then stopped
The application process started but failed to listen on the configured port
Troubleshooting steps:
Check the system event log for error messages
Enable logging the application process' stdout messages
Attach a debugger to the application process and inspect
For more information visit: https://go.microsoft.com/fwlink/?LinkID=808681

I am on .Net Core 2.2. I suspect this is a compatibility issue.

How to override application configuration for this package

I have couple of issues as listed below. It would be great if you help me to resolve these issues.

Issue 1:

I have an Asp.net core 6 web api. I host the application in Azure App Services. I have multiple environments like Dev, Test, Staging and Production.

Usually, I will add all properties to appSettings.json which does not change and I specify an empty string or null for properties that may vary across different environments. Then, I specify those properties in the Azure app service configuration.

In our case, only web hook uri will vary across different environment. So, I added the configuration in appSettings.json file as below. But the application failed to start.

{
  "Name": "MicrosoftTeams",
  "Args": {
	"webHookUri": "",
	"titleTemplate": "{ApplicationName} - {Environment}",
	"batchSizeLimit": 10,
	"period": "00:00:05",
	"restrictedToMinimumLevel": "Warning",
	"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3} {Message}{NewLine}{Exception}",
	"queueLimit": 10
  }
}

I do not want to add bunch of properties to Azure config which does not change. At the same time I do not want to hard code those settings in code.

Let me know if there is a way to fix this issue?

Issue 2:

When the application sends several messages like 50 messages in a min, after a while no messages are sent to teams channel. After some time it starts working again. After googling a lot, found that it may be due to rate limiting with Teams. Is there a way to identify this issue and take any alternative approach can be taken?

Thanks a lot :)

The minimum level setting does not show the data properly.

MinimumLevel isn't working properly (From #1 (comment)):

It works similar to another repo - it outputs everything to the channel. But the moment I put in the minimum level, it does nothing, even if I put in Information or manually tries to log it using logger.LogInformation("Information Test")..

dependency clash on NewtonSoft.JSON

I have a dependency clash on NewtonSoft.JSON, where a product application framework is still using version 10.x (and .net framework 4.5) and this Serilog.Sinks.MicrosoftTeams.Alternative is referencing version 13.x.

Any way to remove the NewtonSoft.JSON dependency and use another JSON serializer or serialize the MicrosoftTeamsMessageCard to JSON by hand?
I tried UTF8Json and even System.Text.Json (>= .net 4,6.2), but I don't want to increase the .NET framework dependency too much..

1.2.13 not working in netcoreapp3.1

Updated from v1.2.11 to v1.2.13.
Fails in Main when creating Logger:
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'

Downgrading to v1.2.12 works but v1.2.13 seems to be broken for netcoreapp3.1.

OmitPropertiesSection in appsettings is ignored

The call from the config looks like this:

MicrosoftTeamsSinkOptions microsoftTeamsSinkOptions = new MicrosoftTeamsSinkOptions(webHookUri, title, batchSizeLimit, period, formatProvider, LogEventLevel.Verbose, false, proxy);

OmitPropertiesSection is always set to false when reading the config.

Customize title text (like outputTemplate)

It would really help if the title text could be customized per log entry similar to the outputTemplate.

We have a multitenant environment, and it would be great to have a title something like,
"{Tenant} {Level} {Message}"

It would be fine if the title was truncated, if there's a limit in Teams.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.