-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorUnitLogger.cs
80 lines (69 loc) · 2.54 KB
/
ErrorUnitLogger.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
using Elmah;
using ErrorUnit.Interfaces;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace ErrorUnit.Logger_Elmah
{
/// <summary>
/// The Elmah ErrorUnit Logger class
/// </summary>
public class ErrorUnitLogger : ILogger
{
/// <summary>
/// Logs the specified ex.
/// </summary>
/// <param name="ex">The ex.</param>
public void Log(Exception ex)
{
var signal = ErrorSignal.FromCurrentContext();
if (signal == null)
return;
signal.Raise(ex);
}
/// <summary>
/// Logs the specified testable error json.
/// </summary>
/// <param name="testableErrorJson">The testable error json.</param>
/// <param name="exception">The exception.</param>
/// <returns></returns>
public string Log(string testableErrorJson, Exception exception)
{
var signal = ErrorSignal.FromCurrentContext();
var log = new ErrorUnitException(testableErrorJson, exception);
if (signal != null)
signal.Raise(log);
return log.ToString();
}
/// <summary>
/// Gets the error unit json after-date.
/// </summary>
/// <param name="afterdate">The after-date.</param>
/// <returns></returns>
public IEnumerable<string> GetErrorUnitJson(DateTime afterdate)
{
var a = new Elmah.ErrorLogDataSourceAdapter();
var errLog = Elmah.ErrorLog.GetDefault(null);
const int rowsize = 10;
var startrow = 0;
var testableErrors = new ConcurrentBag<string>();
ErrorLogEntry[] logentries = null;
while (logentries == null || logentries.Count() == rowsize)
{
logentries = a.GetErrors(startrow, rowsize);
startrow += rowsize;
foreach (var te in logentries.Where(le => le.Error.Time > afterdate))
{
var err = errLog.GetError(te.Id);
var detail = err.Error.Detail?.Trim();
if (detail.EndsWith("}") && detail.Contains(@"""$type"": ""ErrorUnit.Models.TestableError, ErrorUnit"","))
testableErrors.Add(detail.Substring(detail.IndexOf('{')));
}
if (logentries.Any(le => le.Error.Time <= afterdate))
break;
}
return testableErrors;
}
}
}