-
Notifications
You must be signed in to change notification settings - Fork 44
/
RollbarFactory.cs
76 lines (69 loc) · 2.85 KB
/
RollbarFactory.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
namespace Rollbar
{
using System;
using System.Runtime.InteropServices;
using Rollbar.Infrastructure;
/// <summary>
/// RollbarFactory utility class.
/// </summary>
public static class RollbarFactory
{
/// <summary>
/// Creates the proper.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="blockingLoggingTimeout">The blocking logging timeout.</param>
/// <returns>ILogger.</returns>
public static ILogger CreateProper(
IRollbarLoggerConfig? config = null,
TimeSpan? blockingLoggingTimeout = null
)
{
IRollbar rollbar = RollbarFactory.CreateNew(config);
if(blockingLoggingTimeout.HasValue)
{
return rollbar.AsBlockingLogger(blockingLoggingTimeout.Value);
}
else
{
return rollbar.Logger;
}
}
/// <summary>
/// Creates the new instance of IRollbar.
/// </summary>
/// <param name="config">The rollbar configuration.</param>
/// <returns>IRollbar.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Critical Code Smell", "S3973:A conditionally executed single line should be denoted by indentation", Justification = "We have conditional compilation here. Keeps better code structure.")]
public static IRollbar CreateNew(
IRollbarLoggerConfig? config = null
)
{
if(config == null && RollbarInfrastructure.Instance.IsInitialized)
{
config = RollbarInfrastructure.Instance.Config.RollbarLoggerConfig;
}
#if !NETFX_47nOlder
if(RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")))
{
// We are running within Blazor WASM runtime environment that is known to be single threaded in its nature
// at least at the moment, so no background threads are allowed and our infrastructure depends on the threads.
// Hence, let's use a single-threaded logger that does not rely on any infrastructure services:
return new RollbarSingleThreadedLogger(config);
}
else
#endif
if(!RollbarInfrastructure.Instance.IsInitialized)
{
// Regardless of actual runtime environment, it looks like our infrastructure was never initialized.
// Hence, let's use a single-threaded logger that does not rely on any infrastructure services:
return new RollbarSingleThreadedLogger(config);
}
else
{
// It is safe to assume we can take advantage of our pre-initialized infrastructure:
return new RollbarLogger(config);
}
}
}
}