Created
March 31, 2017 07:33
-
-
Save hikalkan/6f573935077d68f1e37b029934ce09e9 to your computer and use it in GitHub Desktop.
How to add a dyanmic tenantid property for log4net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<conversionPattern value="%-5level %date [%-5.5thread] [%property{tenantid}] %-40.40logger - %message%newline" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Abp.Dependency; | |
using Abp.Runtime.Session; | |
namespace Abp.Castle.Logging.Log4Net | |
{ | |
public class Log4NetTenantIdProperty | |
{ | |
private readonly IIocResolver _iocResolver; | |
private IAbpSession _session; | |
public Log4NetTenantIdProperty(IIocResolver iocResolver) | |
{ | |
_iocResolver = iocResolver; | |
} | |
public override string ToString() | |
{ | |
if (_session == null) | |
{ | |
lock (this) | |
{ | |
if (_session == null) | |
{ | |
if (_iocResolver.IsRegistered<IAbpSession>()) | |
{ | |
_session = _iocResolver.Resolve<IAbpSession>(); | |
} | |
} | |
} | |
} | |
if (_session?.TenantId == null) | |
{ | |
return "host"; | |
} | |
return _session.TenantId.ToString(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log4net.GlobalContext.Properties["tenantid"] = new Log4NetTenantIdProperty(IocManager.Instance); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment