Working on parent child for logging
This commit is contained in:
parent
e73f428c49
commit
5568007237
4 changed files with 355 additions and 288 deletions
|
|
@ -49,6 +49,11 @@ namespace Core.Logging
|
||||||
await _seqLogger.LogAsync(et);
|
await _seqLogger.LogAsync(et);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case EventTelemetry et:
|
||||||
|
await _seqLogger.LogAsync(et);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new NotSupportedException(telemetry.GetType().Name);
|
throw new NotSupportedException(telemetry.GetType().Name);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,15 +108,24 @@ namespace Core.Logging
|
||||||
var seqEvent = new Dictionary<string, object>
|
var seqEvent = new Dictionary<string, object>
|
||||||
{
|
{
|
||||||
{ "@t", req.Timestamp.UtcDateTime.ToString("o") },
|
{ "@t", req.Timestamp.UtcDateTime.ToString("o") },
|
||||||
{ "@mt", $"Request: {req.Name}" },
|
{ "@mt",$"{req.Properties["httpMethod"]} {req.Name}" },
|
||||||
{ "@l", req.Success??true ? "Information" : "Error" },
|
{ "@l", req.Success??true ? "Information" : "Error" },
|
||||||
{ "Environment", _environmentName },
|
{ "Environment", _environmentName },
|
||||||
{ "MachineName", _machineName },
|
{ "MachineName", _machineName },
|
||||||
{ "Url", req.Url },
|
{ "Url", req.Url },
|
||||||
{ "ResponseCode", req.ResponseCode },
|
{ "ResponseCode", req.ResponseCode },
|
||||||
{ "Duration", req.Duration.TotalMilliseconds }
|
{ "Application", "sadasd" },
|
||||||
|
{ "RequestMethod", req.Properties["httpMethod"] },
|
||||||
|
{ "RequestId", req.Id },
|
||||||
|
{ "ItemTypeFlag", req.ItemTypeFlag.ToString() },
|
||||||
|
{ "@sp", "12"},
|
||||||
|
{ "@tr", "23344"},
|
||||||
|
{ "@sk","Server" },
|
||||||
|
{ "@st", req.Timestamp.UtcDateTime.Subtract(req.Duration).ToString("o") }
|
||||||
};
|
};
|
||||||
|
req.Properties.Remove("httpMethod");
|
||||||
|
|
||||||
|
//we should add a property with name { "Application", "<...>" } other the Seq Span is not looking ok
|
||||||
foreach (var prop in req.Properties)
|
foreach (var prop in req.Properties)
|
||||||
{
|
{
|
||||||
seqEvent.Add(prop.Key, prop.Value);
|
seqEvent.Add(prop.Key, prop.Value);
|
||||||
|
|
@ -124,7 +133,39 @@ namespace Core.Logging
|
||||||
|
|
||||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||||
}
|
}
|
||||||
|
public async Task LogAsync(Microsoft.ApplicationInsights.Extensibility.IOperationHolder<RequestTelemetry> operationHolder, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var req = operationHolder.Telemetry;
|
||||||
|
|
||||||
|
var seqEvent = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
|
||||||
|
{ "@t", req.Timestamp.UtcDateTime.ToString("o") },
|
||||||
|
{ "@mt",$"{req.Properties["httpMethod"]} {req.Name}" },
|
||||||
|
{ "@l", req.Success??true ? "Information" : "Error" },
|
||||||
|
{ "Environment", _environmentName },
|
||||||
|
{ "MachineName", _machineName },
|
||||||
|
{ "Url", req.Url },
|
||||||
|
{ "ResponseCode", req.ResponseCode },
|
||||||
|
{ "Application", "sadasd" },
|
||||||
|
{ "RequestMethod", req.Properties["httpMethod"] },
|
||||||
|
{ "RequestId", req.Id },
|
||||||
|
{ "ItemTypeFlag", req.ItemTypeFlag.ToString() },
|
||||||
|
{ "@sp", "12"},
|
||||||
|
{ "@tr", "23344"},
|
||||||
|
{ "@sk","Server" },
|
||||||
|
{ "@st", req.Timestamp.UtcDateTime.Subtract(req.Duration).ToString("o") }
|
||||||
|
};
|
||||||
|
req.Properties.Remove("httpMethod");
|
||||||
|
|
||||||
|
//we should add a property with name { "Application", "<...>" } other the Seq Span is not looking ok
|
||||||
|
foreach (var prop in req.Properties)
|
||||||
|
{
|
||||||
|
seqEvent.Add(prop.Key, prop.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||||
|
}
|
||||||
private async Task SendToSeqAsync(Dictionary<string, object> seqEvent, CancellationToken cancellationToken)
|
private async Task SendToSeqAsync(Dictionary<string, object> seqEvent, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var content = new StringContent(
|
var content = new StringContent(
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,14 @@ namespace Tests.Logging
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++)
|
for (int i = 0; i < 5; i++)
|
||||||
{
|
{
|
||||||
var eventTelemetry = new EventTelemetry("SomeEvent");
|
var eventTelemetry = new EventTelemetry
|
||||||
|
{
|
||||||
|
Name = "Test Event",
|
||||||
|
Timestamp = DateTimeOffset.UtcNow
|
||||||
|
};
|
||||||
|
eventTelemetry.Properties.Add("TestId", Guid.NewGuid().ToString());
|
||||||
|
eventTelemetry.Metrics.Add("TestMetric", 42.0);
|
||||||
|
|
||||||
await _messageChannel.Writer.WriteAsync(eventTelemetry);
|
await _messageChannel.Writer.WriteAsync(eventTelemetry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
using Core.Logging;
|
using Autofac;
|
||||||
|
using Core.Logging;
|
||||||
|
using Microsoft.ApplicationInsights;
|
||||||
using Microsoft.ApplicationInsights.DataContracts;
|
using Microsoft.ApplicationInsights.DataContracts;
|
||||||
|
|
||||||
namespace Tests.Logging
|
namespace Tests.Logging
|
||||||
|
|
@ -113,20 +115,32 @@ namespace Tests.Logging
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task LogRequestTelemetry_SendsCorrectData()
|
public async Task LogRequestTelemetry_SendsCorrectData()
|
||||||
{
|
{
|
||||||
// Arrange
|
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||||
var requestTelemetry = new RequestTelemetry
|
|
||||||
{
|
|
||||||
Name = "GET /api/test",
|
|
||||||
Success = true,
|
|
||||||
ResponseCode = "200",
|
|
||||||
Duration = TimeSpan.FromMilliseconds(50),
|
|
||||||
Url = new Uri("http://test.com/api/test"),
|
|
||||||
Timestamp = DateTimeOffset.UtcNow
|
|
||||||
};
|
|
||||||
requestTelemetry.Properties.Add("TestId", _testId);
|
|
||||||
|
|
||||||
|
var operationId = "op123";
|
||||||
|
|
||||||
|
using (Microsoft.ApplicationInsights.Extensibility.IOperationHolder<RequestTelemetry> operation = telemetryClient.StartOperation<RequestTelemetry>("GET /api/parent"))
|
||||||
|
{
|
||||||
|
|
||||||
|
using (var child = telemetryClient.StartOperation<RequestTelemetry>("api/test"))
|
||||||
|
// Arrange
|
||||||
|
{
|
||||||
|
|
||||||
|
//child.Telemetry.Name = "/api/test";
|
||||||
|
child.Telemetry.Success = true;
|
||||||
|
child.Telemetry.ResponseCode = "200";
|
||||||
|
child.Telemetry.Duration = TimeSpan.FromMilliseconds(50);
|
||||||
|
child.Telemetry.Url = new Uri("http://test.com/api/test");
|
||||||
|
child.Telemetry.Timestamp = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
|
child.Telemetry.Properties.Add("httpMethod", HttpMethod.Get.ToString());
|
||||||
|
child.Telemetry.Properties.Add("TestId", _testId);
|
||||||
|
|
||||||
|
await _logger.LogAsync(child);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
// Act
|
// Act
|
||||||
await _logger.LogAsync(requestTelemetry);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue