Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Created March 31, 2017 07:33
Show Gist options
  • Save hikalkan/6f573935077d68f1e37b029934ce09e9 to your computer and use it in GitHub Desktop.
Save hikalkan/6f573935077d68f1e37b029934ce09e9 to your computer and use it in GitHub Desktop.
How to add a dyanmic tenantid property for log4net
<conversionPattern value="%-5level %date [%-5.5thread] [%property{tenantid}] %-40.40logger - %message%newline" />
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();
}
}
}
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