-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<Authors>Ray</Authors> | ||
<Version>0.0.7</Version> | ||
<Version>0.0.8</Version> | ||
<NoWarn>$(NoWarn);CS1591;CS0436</NoWarn> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsApiClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using Ray.Serilog.Sinks.Batched; | ||
|
||
namespace Ray.Serilog.Sinks.MicrosoftTeams | ||
{ | ||
public class MicrosoftTeamsApiClient : PushService | ||
{ | ||
//https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook | ||
|
||
private readonly Uri _apiUrl; | ||
private readonly HttpClient _httpClient = new HttpClient(); | ||
|
||
public MicrosoftTeamsApiClient( | ||
string webhook | ||
) | ||
{ | ||
_apiUrl = new Uri(webhook); | ||
} | ||
|
||
public override string ClientName => "MicrosoftTeams"; | ||
|
||
protected override string NewLineStr => "<br/>"; | ||
|
||
public override HttpResponseMessage DoSend() | ||
{ | ||
var json = new | ||
{ | ||
text=Msg | ||
}.ToJson(); | ||
|
||
var content = new StringContent(json, Encoding.UTF8, "application/json"); | ||
|
||
var response = _httpClient.PostAsync(_apiUrl, content).GetAwaiter().GetResult(); | ||
return response; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsBatchedSink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using Ray.Serilog.Sinks.Batched; | ||
using Serilog.Events; | ||
|
||
namespace Ray.Serilog.Sinks.MicrosoftTeams | ||
{ | ||
public class MicrosoftTeamsBatchedSink : BatchedSink | ||
{ | ||
private readonly string _webhook; | ||
|
||
public MicrosoftTeamsBatchedSink( | ||
string webhook, | ||
Predicate<LogEvent> predicate, | ||
bool sendBatchesAsOneMessages, | ||
string outputTemplate, | ||
IFormatProvider formatProvider, | ||
LogEventLevel minimumLogEventLevel | ||
) | ||
: base(predicate, sendBatchesAsOneMessages, outputTemplate, formatProvider, minimumLogEventLevel) | ||
{ | ||
_webhook = webhook; | ||
} | ||
|
||
public override void Emit(LogEvent logEvent) | ||
{ | ||
if (_webhook.IsNullOrEmpty()) return; | ||
base.Emit(logEvent); | ||
} | ||
|
||
protected override PushService PushService => new MicrosoftTeamsApiClient( | ||
webhook: _webhook); | ||
|
||
public override void Dispose() | ||
{ | ||
//todo | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...y.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/MicrosoftTeamsConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using Ray.Serilog.Sinks.Batched; | ||
using Serilog; | ||
using Serilog.Configuration; | ||
using Serilog.Events; | ||
|
||
namespace Ray.Serilog.Sinks.MicrosoftTeams | ||
{ | ||
public static class MicrosoftTeamsConfigurationExtensions | ||
{ | ||
public static LoggerConfiguration MicrosoftTeamsBatched( | ||
this LoggerSinkConfiguration loggerSinkConfiguration, | ||
string webhook = "", | ||
string containsTrigger = Constants.DefaultContainsTrigger, | ||
bool sendBatchesAsOneMessages = true, | ||
string outputTemplate = Constants.DefaultOutputTemplate, | ||
IFormatProvider formatProvider = null, | ||
LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose | ||
) | ||
{ | ||
if (loggerSinkConfiguration == null) | ||
throw new ArgumentNullException(nameof(loggerSinkConfiguration)); | ||
if (outputTemplate == null) | ||
throw new ArgumentNullException(nameof(outputTemplate)); | ||
|
||
if (containsTrigger.IsNullOrEmpty()) containsTrigger = Constants.DefaultContainsTrigger; | ||
Predicate<LogEvent> predicate = x => x.MessageTemplate.Text.Contains(containsTrigger); | ||
|
||
return loggerSinkConfiguration.Sink( | ||
new MicrosoftTeamsBatchedSink( | ||
webhook, | ||
predicate, | ||
sendBatchesAsOneMessages, | ||
outputTemplate, | ||
formatProvider, | ||
restrictedToMinimumLevel), | ||
restrictedToMinimumLevel); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ay.Serilog.Sinks/Ray.Serilog.Sinks.MicrosoftTeams/Ray.Serilog.Sinks.MicrosoftTeams.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Ray.BiliBiliTool.Infrastructure\Ray.BiliBiliTool.Infrastructure.csproj" /> | ||
<ProjectReference Include="..\Ray.Serilog.Sinks.Batched\Ray.Serilog.Sinks.Batched.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Ray.BiliBiliTool.Console; | ||
using Ray.BiliBiliTool.Infrastructure; | ||
using Ray.Serilog.Sinks.CoolPushBatched; | ||
using Ray.Serilog.Sinks.MicrosoftTeams; | ||
using Ray.Serilog.Sinks.PushPlusBatched; | ||
using Ray.Serilog.Sinks.ServerChanBatched; | ||
using Xunit; | ||
|
||
namespace LogTest | ||
{ | ||
public class TestMicrosoftTeams | ||
{ | ||
private string _webhook; | ||
|
||
public TestMicrosoftTeams() | ||
{ | ||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); | ||
Program.CreateHost(new string[] { }); | ||
|
||
_webhook = Global.ConfigurationRoot["Serilog:WriteTo:10:Args:webhook"]; | ||
} | ||
|
||
[Fact] | ||
public void Test() | ||
{ | ||
var client = new MicrosoftTeamsApiClient(webhook: _webhook); | ||
|
||
var msg = LogConstants.Msg2; | ||
|
||
var result = client.PushMessage(msg); | ||
Debug.WriteLine(result.Content.ReadAsStringAsync().Result); | ||
|
||
Assert.True(result.StatusCode == System.Net.HttpStatusCode.OK); | ||
} | ||
} | ||
} |