-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDaprLoggerFactoryBase.cs
41 lines (35 loc) · 1.48 KB
/
DaprLoggerFactoryBase.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
// Based on https://github.com/dotnet/extensions/blob/release/2.1/src/Logging/Logging/src/LoggerFactory.cs
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See NET_EXTENSIONS_LICENSE in this directory for license information.
using System;
using System.Collections.Generic;
namespace Man.Dapr.Sidekick.Logging
{
/// <summary>
/// Base class for <see cref="IDaprLoggerFactory"/> implementations.
/// </summary>
public abstract class DaprLoggerFactoryBase : DaprDisposable, IDaprLoggerFactory
{
private readonly Dictionary<string, IDaprLogger> _loggers = new Dictionary<string, IDaprLogger>(StringComparer.Ordinal);
private readonly object _sync = new object();
public IDaprLogger CreateLogger(string categoryName)
{
EnsureNotDisposed();
// Cannot use ConcurrentDictionary as need to support net35.
// Standard check-lock-check approach
if (!_loggers.TryGetValue(categoryName, out var logger))
{
lock (_sync)
{
if (!_loggers.TryGetValue(categoryName, out logger))
{
logger = CreateLoggerImpl(categoryName);
_loggers[categoryName] = logger;
}
}
}
return logger;
}
protected abstract IDaprLogger CreateLoggerImpl(string categoryName);
}
}