Skip to content

Commit

Permalink
Use IReportEventsObserver instead of obsoleted ILogFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Jan 22, 2022
1 parent 3874adb commit 55e22ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ReportPortal.Shared" Version="3.0.0" />
<PackageReference Include="ReportPortal.Shared" Version="3.2.0" />
<PackageReference Include="System.Reflection.Metadata" Version="1.8.0" />
</ItemGroup>

Expand Down
34 changes: 20 additions & 14 deletions src/ReportPortal.Extensions.SourceBack/SourceBackFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ReportPortal.Extensions.SourceBack.Pdb;
using ReportPortal.Shared.Configuration;
using ReportPortal.Shared.Extensibility;
using ReportPortal.Shared.Extensibility.ReportEvents;
using ReportPortal.Shared.Internal.Logging;
using System;
using System.Collections.Generic;
Expand All @@ -14,7 +15,7 @@

namespace ReportPortal.Extensions.SourceBack
{
public class SourceBackFormatter : ILogFormatter
public class SourceBackFormatter : IReportEventsObserver
{
private readonly ITraceLogger _traceLogger = TraceLogManager.Instance.GetLogger<SourceBackFormatter>();

Expand All @@ -24,15 +25,27 @@ public SourceBackFormatter()
Config = new ConfigurationBuilder().AddDefaults(configDirectory).Build();
}

public int Order => 10;

private IConfiguration Config { get; }

public bool FormatLog(CreateLogItemRequest logRequest)
public void Initialize(IReportEventsSource reportEventsSource)
{
_traceLogger.Verbose("Received a log request to format.");
reportEventsSource.OnBeforeLogsSending += ReportEventsSource_OnBeforeLogsSending;
}

private void ReportEventsSource_OnBeforeLogsSending(Shared.Reporter.ILogsReporter logsReporter, Shared.Extensibility.ReportEvents.EventArgs.BeforeLogsSendingEventArgs args)
{
if (args.CreateLogItemRequests != null)
{
foreach (var createLogItemRequest in args.CreateLogItemRequests)
{
FormatLog(createLogItemRequest);
}
}
}

var handled = false;
public void FormatLog(CreateLogItemRequest logRequest)
{
_traceLogger.Verbose("Received a log request to format.");

var fullMessageBuilder = Config.GetValue("Extensions:SourceBack:WithMarkdownPrefix", false) ? new StringBuilder("!!!MARKDOWN_MODE!!!") : new StringBuilder();

Expand Down Expand Up @@ -132,8 +145,6 @@ public bool FormatLog(CreateLogItemRequest logRequest)
sectionBuilder.AppendLine($"```{Environment.NewLine}SourceBack error: {exp}{Environment.NewLine}```");
}

handled = true;

if (!string.IsNullOrEmpty(sectionBuilder.ToString()))
{
var sourceFileName = Path.GetFileName(sourcePath);
Expand Down Expand Up @@ -162,12 +173,7 @@ public bool FormatLog(CreateLogItemRequest logRequest)
}
}

if (handled)
{
logRequest.Text = fullMessageBuilder.ToString();
}

return handled;
logRequest.Text = fullMessageBuilder.ToString();
}

private static readonly object _pdbsLock = new object();
Expand Down

0 comments on commit 55e22ad

Please sign in to comment.