-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogService.cs
117 lines (99 loc) · 4.02 KB
/
LogService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Xamling.Azure.Portable.Contract;
using Xamling.Azure.Portable.Entity;
using XamlingCore.Portable.Contract.Serialise;
using XamlingCore.Portable.Contract.Services;
using XamlingCore.Portable.Model.Other;
using XamlingCore.Portable.Model.Response;
namespace Xamling.Azure.Logger
{
public class LogService : ILogService
{
private readonly IEntitySerialiser _entitySerialiser;
private readonly IContextInfoService _contextInfoService;
private readonly TelemetryClient _telemetry;
public LogService(IEntitySerialiser entitySerialiser,
IContextInfoService contextInfoService)
{
_entitySerialiser = entitySerialiser;
_contextInfoService = contextInfoService;
_telemetry = new TelemetryClient();
}
public void TrackEvent(string eventName, IDictionary<string, string> properties = null,
IDictionary<string, double> metrics = null)
{
_telemetry.TrackEvent(eventName, properties, metrics);
}
public void TrackException(Exception exception, IDictionary<string, string> properties = null,
IDictionary<string, double> metrics = null)
{
_telemetry.TrackException(exception, properties, metrics);
}
public void TrackMetric(string name, double value, IDictionary<string, string> properties = null)
{
_telemetry.TrackMetric(name, value, properties);
}
public void TrackPageView(string name)
{
_telemetry.TrackPageView(name);
}
public void TrackRequest(string name, DateTimeOffset timestamp, TimeSpan duration, string responseCode,
bool success)
{
_telemetry.TrackRequest(name, timestamp, duration, responseCode, success);
}
public void TrackTrace(string message)
{
_telemetry.TrackTrace(message);
}
public void TrackTrace(string message, IDictionary<string, string> properties)
{
_telemetry.TrackTrace(message, properties);
}
public void TrackTrace(string message, XSeverityLevel severityLevel)
{
_telemetry.TrackTrace(message, Mapper.Map<SeverityLevel>(severityLevel));
}
public void TrackTrace(string message, XSeverityLevel severityLevel, IDictionary<string, string> properties)
{
_telemetry.TrackTrace(message, Mapper.Map<SeverityLevel>(severityLevel), properties);
}
public XResult<T> TrackOperation<T>(XResult<T> operation, string operationName = null)
{
var op = _entitySerialiser.Serialise(operation);
var dict = new Dictionary<string, string>();
if (operationName != null)
{
dict.Add("Name", operationName);
}
dict.Add("Id", operation.Id.ToString());
dict.Add("Message", operation.Message);
dict.Add("CallerMemberName", operation.CallerInfo.MemberName);
dict.Add("Result", operation.ResultCode.ToString());
dict.Add("StatusCode", operation.StatusCode.ToString());
dict.Add("XResult", op);
if (_contextInfoService.Context != null)
{
foreach (var i in _contextInfoService.Context)
{
dict.Add(i.Key, i.Value);
}
}
if (operation.ResultCode == OperationResults.Exception || operation.Exception != null)
{
dict.Add("ExceptionType", "XResult");
TrackException(operation.Exception, dict);
return operation;
}
TrackTrace("XResult", operation.IsSuccess ? XSeverityLevel.Information : XSeverityLevel.Error, dict);
return operation;
}
}
}