Skip to content

Commit

Permalink
Code refactoring. Update package version.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-gubskiy committed Nov 5, 2021
1 parent ba631e6 commit 67b86d3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 27 deletions.
8 changes: 5 additions & 3 deletions examples/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ static void Main(string[] args)
{
AccessToken = "1234567890:AAAaaAAaa_AaAAaa-AAaAAAaAAaAaAaAAAA",
ChatId = "-0000000000000",
LogLevel = LogLevel.Information
LogLevel = LogLevel.Information,
Source = "TEST APP",
UseEmoji = true
};

var factory = LoggerFactory.Create(builder =>
Expand All @@ -44,8 +46,8 @@ static void Main(string[] args)

try
{
throw new SystemException("Exception message description");

throw new SystemException("Exception message description. <br /> This message contains " +
"<html> <tags /> And some **special** symbols _");
}
catch (Exception exception)
{
Expand Down
19 changes: 16 additions & 3 deletions src/X.Extensions.Logging.Telegram/TelegramLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ public class TelegramLogger : ILogger
{
private readonly TelegramLoggerProcessor _queueProcessor;
private readonly string _category;
private readonly TelegramMessageFormatter _formatter;
private readonly ITelegramMessageFormatter _formatter;

internal TelegramLogger(string name, TelegramLoggerOptions options, TelegramLoggerProcessor loggerProcessor, string category)
internal TelegramLogger(
string name,
TelegramLoggerOptions options,
TelegramLoggerProcessor loggerProcessor,
string category)
: this(options, loggerProcessor, category, new TelegramMessageFormatter(options, name))
{
}

internal TelegramLogger(
TelegramLoggerOptions options,
TelegramLoggerProcessor loggerProcessor,
string category,
ITelegramMessageFormatter formatter)
{
_queueProcessor = loggerProcessor;
_category = category;
_formatter = new TelegramMessageFormatter(options, name);
_formatter = formatter;

Options = options ?? throw new ArgumentNullException(nameof(options));
}
Expand Down
33 changes: 26 additions & 7 deletions src/X.Extensions.Logging.Telegram/TelegramMessageFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
using System;
using System.Net;
using System.Text;

using JetBrains.Annotations;
using Microsoft.Extensions.Logging;

namespace X.Extensions.Logging.Telegram
{
public class TelegramMessageFormatter
[PublicAPI]
public interface ITelegramMessageFormatter
{
string Format<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter);

string Format(LogLevel logLevel, Exception exception, string message);

string EncodeHtml(string text);
}

[PublicAPI]
public class TelegramMessageFormatter : ITelegramMessageFormatter
{
private readonly TelegramLoggerOptions _options;
private readonly string _name;
Expand All @@ -22,10 +38,11 @@ public string Format<TState>(
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
{
var message = formatter(state, exception);
Func<TState, Exception, string> formatter) =>
Format(logLevel, exception, formatter(state, exception));

public virtual string Format(LogLevel logLevel, Exception exception, string message)
{
if (string.IsNullOrWhiteSpace(message))
{
return string.Empty;
Expand All @@ -41,13 +58,13 @@ public string Format<TState>(
sb.Append($"<pre>{_name}</pre>");

sb.AppendLine();
sb.AppendLine($"Message: {message}");
sb.AppendLine($"Message: {EncodeHtml(message)}");
sb.AppendLine();

if (exception != null)
{
sb.AppendLine();
sb.AppendLine($"<pre>{WebUtility.HtmlEncode(exception.ToString())}</pre>");
sb.AppendLine($"<pre>{EncodeHtml(exception.ToString())}</pre>");
sb.AppendLine();
}

Expand All @@ -62,6 +79,8 @@ public string Format<TState>(
return sb.ToString();
}

public virtual string EncodeHtml(string text) => WebUtility.HtmlEncode(text);

private static string ToString(LogLevel level) =>
level switch
{
Expand Down
9 changes: 7 additions & 2 deletions src/X.Extensions.Logging.Telegram/TelegramWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ public interface ITelegramWriter
public class TelegramWriter : ITelegramWriter
{
private readonly string _chatId;
private readonly TelegramBotClient _client;
private readonly ITelegramBotClient _client;

public TelegramWriter(string accessToken, string chatId)
: this(new TelegramBotClient(accessToken), chatId)
{
}

public TelegramWriter(ITelegramBotClient client, string chatId)
{
_chatId = chatId;
_client = new TelegramBotClient(accessToken);
_client = client;
}

public async Task Write(string message) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageTags>telegram, logging, log, logs, tg</PackageTags>
<Description>Telegram logging provider</Description>
<RepositoryType>git</RepositoryType>
<Version>1.0.2.0</Version>
<Version>1.0.2.1</Version>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

Expand Down
29 changes: 18 additions & 11 deletions tests/X.Extensions.Logging.Telegram.Tests/TelegramLoggingTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Net;
using System.Text.Encodings.Web;

using Microsoft.Extensions.Logging;
using NUnit.Framework;

Expand Down Expand Up @@ -36,15 +33,25 @@ public void Test_MessageFormatter_MessageNotNull()
[TestCase("<p style=\"font-family='Lucida Console';width:100%\">Exception <br/><i><b>message</b></i> description</p>")]
public void ExceptionDescriptionWithRawHtmlTest(string description)
{
var encodedHtml = WebUtility.HtmlEncode(description);
ITelegramMessageFormatter formatter = new TelegramMessageFormatter(new TelegramLoggerOptions
{
Categories = new[] { "test" },
Source = "Test API",
AccessToken = "none",
ChatId = "12345",
LogLevel = LogLevel.Information,
UseEmoji = true
}, "test");

var containsRawHtml = encodedHtml.Contains("<p style=\"font-family='Lucida Console'\">") ||
encodedHtml.Contains("</p>") ||
encodedHtml.Contains("<br/>") ||
encodedHtml.Contains("<i>") ||
encodedHtml.Contains("</i>") ||
encodedHtml.Contains("<b>") ||
encodedHtml.Contains("</b>");
var result = formatter.EncodeHtml(description);

var containsRawHtml = result.Contains("<p style=\"font-family='Lucida Console'\">") ||
result.Contains("</p>") ||
result.Contains("<br/>") ||
result.Contains("<i>") ||
result.Contains("</i>") ||
result.Contains("<b>") ||
result.Contains("</b>");

Assert.False(containsRawHtml);
}
Expand Down

0 comments on commit 67b86d3

Please sign in to comment.