Skip to content

Commit

Permalink
Merge pull request #5 from Bardin08/bardin08/issues/4
Browse files Browse the repository at this point in the history
Replace markdown with html
  • Loading branch information
ernado-x authored Nov 5, 2021
2 parents 1067ada + edf4f2f commit ba631e6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/X.Extensions.Logging.Telegram/TelegramMessageFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Net;
using System.Text;

using Microsoft.Extensions.Logging;

namespace X.Extensions.Logging.Telegram
Expand Down Expand Up @@ -32,11 +34,11 @@ public string Format<TState>(
var sb = new StringBuilder();

sb.Append(_options.UseEmoji
? $"{ToEmoji(logLevel)} *{DateTime.Now:HH:mm:ss}* {ToString(logLevel)}"
: $"*{DateTime.Now:HH:mm:ss}* {ToString(logLevel)}");
? $"{ToEmoji(logLevel)} <b>{DateTime.Now:HH:mm:ss}</b> {ToString(logLevel)}"
: $"<b>{DateTime.Now:HH:mm:ss}</b> {ToString(logLevel)}");

sb.AppendLine();
sb.Append($"`{_name}`");
sb.Append($"<pre>{_name}</pre>");

sb.AppendLine();
sb.AppendLine($"Message: {message}");
Expand All @@ -45,14 +47,14 @@ public string Format<TState>(
if (exception != null)
{
sb.AppendLine();
sb.AppendLine($"`{exception}`");
sb.AppendLine($"<pre>{WebUtility.HtmlEncode(exception.ToString())}</pre>");
sb.AppendLine();
}

if (!string.IsNullOrWhiteSpace(_options.Source))
{
sb.AppendLine();
sb.Append($"_Source: {_options.Source}_");
sb.Append($"<i>Source: {_options.Source}</i>");
}

sb.AppendLine();
Expand Down
2 changes: 1 addition & 1 deletion src/X.Extensions.Logging.Telegram/TelegramWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public TelegramWriter(string accessToken, string chatId)
}

public async Task Write(string message) =>
await _client.SendTextMessageAsync(_chatId, message, ParseMode.Markdown);
await _client.SendTextMessageAsync(_chatId, message, ParseMode.Html);
}
}
23 changes: 21 additions & 2 deletions tests/X.Extensions.Logging.Telegram.Tests/TelegramLoggingTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Net;
using System.Text.Encodings.Web;

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

Expand All @@ -13,7 +15,7 @@ public void Setup()
}

[Test]
public void Test1()
public void Test_MessageFormatter_MessageNotNull()
{
var options = new TelegramLoggerOptions
{
Expand All @@ -29,5 +31,22 @@ public void Test1()

Assert.NotNull(message);
}

[TestCase("<p style=\"font-family='Lucida Console'\">Exception message description</p>")]
[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);

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>");

Assert.False(containsRawHtml);
}
}
}

0 comments on commit ba631e6

Please sign in to comment.