Skip to content

Commit

Permalink
Log: fix configuring
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbannov committed Jul 13, 2021
1 parent 8d28724 commit ad57fee
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 87 deletions.
9 changes: 1 addition & 8 deletions common/ASC.Api.Core/Core/BaseStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using ASC.Common.Mapping;

using Autofac;
Expand All @@ -33,8 +32,7 @@ namespace ASC.Api.Core
public abstract class BaseStartup
{
public IConfiguration Configuration { get; }
public IHostEnvironment HostEnvironment { get; }
public virtual string[] LogParams { get; }
public IHostEnvironment HostEnvironment { get; }
public virtual JsonConverter[] Converters { get; }
public virtual bool ConfirmAddScheme { get; } = false;
protected DIHelper DIHelper { get; }
Expand Down Expand Up @@ -111,11 +109,6 @@ public virtual void ConfigureServices(IServiceCollection services)
if (ConfirmAddScheme)
{
authBuilder.AddScheme<AuthenticationSchemeOptions, ConfirmAuthHandler>("confirm", a => { });
}

if (LogParams != null)
{
LogNLogExtension.ConfigureLog(DIHelper, LogParams);
}

services.AddAutoMapper(Assembly.GetAssembly(typeof(MappingProfile)));
Expand Down
64 changes: 37 additions & 27 deletions common/ASC.Common/Logging/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public interface ILog
void FatalFormat(IFormatProvider provider, string format, params object[] args);

string LogDirectory { get; }
string Name { get; set; }
string Name { get; set; }

void Configure(string name);
}

public class Log : ILog
Expand All @@ -108,7 +110,7 @@ static Log()
XmlConfigurator.Configure(log4net.LogManager.GetRepository(Assembly.GetCallingAssembly()));
}

private readonly log4net.ILog loger;
private log4net.ILog loger;

public bool IsDebugEnabled { get; private set; }

Expand All @@ -124,15 +126,20 @@ static Log()

public Log(string name)
{
Configure(name);
}

public void Configure(string name)
{
loger = log4net.LogManager.GetLogger(Assembly.GetCallingAssembly(), name);

IsDebugEnabled = loger.IsDebugEnabled;
IsInfoEnabled = loger.IsInfoEnabled;
IsWarnEnabled = loger.IsWarnEnabled;
IsErrorEnabled = loger.IsErrorEnabled;
IsFatalEnabled = loger.IsFatalEnabled;
IsTraceEnabled = loger.Logger.IsEnabledFor(Level.Trace);
}
IsTraceEnabled = loger.Logger.IsEnabledFor(Level.Trace);
}

public void Trace(object message)
{
Expand Down Expand Up @@ -401,14 +408,21 @@ public ConfigureLogNLog(IConfiguration configuration, ConfigurationExtension con
NLog.Targets.Target.Register<SelfCleaningTarget>("SelfCleaning");
}

public void Configure(LogNLog options)
{

public void Configure(LogNLog options)
{
options.Configure("ASC");
}

public void Configure(string name, LogNLog options)
{
Configure(options);
if (string.IsNullOrEmpty(name))
{
Configure(options);
}
else
{
options.Configure(name);
}
}
}

Expand Down Expand Up @@ -443,7 +457,12 @@ private NLog.ILogger Loger

public bool IsFatalEnabled { get; private set; }

public bool IsTraceEnabled { get; private set; }
public bool IsTraceEnabled { get; private set; }

public void Configure(string name)
{
Name = name;
}

public void Trace(object message)
{
Expand Down Expand Up @@ -850,19 +869,23 @@ public void FatalFormat(IFormatProvider provider, string format, params object[]

public void DebugWithProps(string message, KeyValuePair<string, object> prop1, KeyValuePair<string, object> prop2, KeyValuePair<string, object> prop3)
{
}

}

public void Configure(string name)
{
}

public string LogDirectory { get { return ""; } }

public string Name { get; set; }
}


[Singletone]
public class LogManager<T> : OptionsMonitor<T> where T : class, ILog, new()
{
public LogManager(IOptionsFactory<T> factory, IEnumerable<IOptionsChangeTokenSource<T>> sources, IOptionsMonitorCache<T> cache) : base(factory, sources, cache)
{
{

}

public override T Get(string name)
Expand All @@ -881,21 +904,8 @@ public override T Get(string name)
public class LoggerExtension<T> where T : class, ILog, new()
{
public static void RegisterLog(DIHelper services)
{
const string baseName = "ASC";
var baseSqlName = $"{baseName}.SQL";
services.Configure<T>(r => r.Name = baseName);
services.Configure<T>(baseName, r => r.Name = baseName);
services.Configure<T>(baseSqlName, r => r.Name = baseSqlName);
services.TryAdd(typeof(IOptionsMonitor<ILog>), typeof(LogManager<T>));
}

public static void ConfigureLog(DIHelper services, params string[] additionalLoggers)
{
foreach (var l in additionalLoggers)
{
services.Configure<T>(l, r => r.Name = l);
}
services.TryAdd(typeof(IOptionsMonitor<ILog>), typeof(LogManager<T>));
}
}

Expand Down
3 changes: 0 additions & 3 deletions common/services/ASC.ApiSystem/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;

using Autofac;

Expand Down Expand Up @@ -82,8 +81,6 @@ public override void ConfigureServices(IServiceCollection services)
services.AddAuthentication()
.AddScheme<AuthenticationSchemeOptions, AuthHandler>("auth.allowskip", _ => { })
.AddScheme<AuthenticationSchemeOptions, AuthHandler>("auth.allowskip.registerportal", _ => { });

LogNLogExtension.ConfigureLog(diHelper, "ASC.Apisystem");
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
2 changes: 0 additions & 2 deletions common/services/ASC.ClearEvents/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using ASC.Common.Utils;

using Autofac;
Expand Down Expand Up @@ -84,7 +83,6 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
var diHelper = new DIHelper(services);

diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
LogNLogExtension.ConfigureLog(diHelper, "ASC.ClearEvents");
services.AddHostedService<ClearEventsServiceLauncher>();
diHelper.TryAdd<ClearEventsServiceLauncher>();
})
Expand Down
2 changes: 0 additions & 2 deletions common/services/ASC.Data.Backup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ namespace ASC.Data.Backup
{
public class Startup : BaseStartup
{
public override string[] LogParams { get => new string[] { "ASC.Data.Backup" }; }

public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment) : base(configuration, hostEnvironment)
{

Expand Down
2 changes: 0 additions & 2 deletions common/services/ASC.Data.Storage.Encryption/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ namespace ASC.Data.Storage.Encryption
{
public class Startup : BaseStartup
{
public override string[] LogParams { get => new string[] { "ASC.Data.Storage.Encryption" }; }

public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment) : base(configuration, hostEnvironment)
{

Expand Down
2 changes: 0 additions & 2 deletions common/services/ASC.Notify/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using ASC.Common.Utils;
using ASC.Core.Notify.Senders;
using ASC.Notify.Config;
Expand Down Expand Up @@ -67,7 +66,6 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
services.AddMemoryCache();
var diHelper = new DIHelper(services);

LogNLogExtension.ConfigureLog(diHelper, "ASC.Notify", "ASC.Notify.Messages");
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);

Expand Down
4 changes: 1 addition & 3 deletions common/services/ASC.Socket.IO.Svc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using ASC.Common.Utils;

using Autofac;
Expand Down Expand Up @@ -93,8 +92,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
var diHelper = new DIHelper(services);
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);

LogNLogExtension.ConfigureLog(diHelper, "ASC.Socket.IO.Svc");

services.AddHostedService<SocketServiceLauncher>();
diHelper.TryAdd<SocketServiceLauncher>();
})
Expand Down
4 changes: 1 addition & 3 deletions common/services/ASC.SsoAuth.Svc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using ASC.Common.Utils;
using ASC.SsoAuth.Svc;

Expand Down Expand Up @@ -94,8 +93,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
var diHelper = new DIHelper(services);
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);

LogNLogExtension.ConfigureLog(diHelper, "ASC.SsoAuth.Svc");

services.AddHostedService<Launcher>();
diHelper.TryAdd<Launcher>();
})
Expand Down
2 changes: 0 additions & 2 deletions common/services/ASC.Studio.Notify/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using ASC.Common.Utils;
using ASC.Core.Notify;
using ASC.Notify;
Expand Down Expand Up @@ -69,7 +68,6 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
var diHelper = new DIHelper(services);
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
diHelper.RegisterProducts(hostContext.Configuration, hostContext.HostingEnvironment.ContentRootPath);
LogNLogExtension.ConfigureLog(diHelper, "ASC.Notify", "ASC.Notify.Messages");
services.AddHostedService<ServiceLauncher>();
diHelper.TryAdd<ServiceLauncher>();
NotifyConfigurationExtension.Register(diHelper);
Expand Down
2 changes: 0 additions & 2 deletions common/services/ASC.TelegramService/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ namespace ASC.TelegramService
{
public class Startup : BaseStartup
{
public override string[] LogParams { get => new string[] { "ASC.TelegramService" }; }

public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment) : base(configuration, hostEnvironment)
{

Expand Down
4 changes: 1 addition & 3 deletions common/services/ASC.Thumbnails.Svc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using ASC.Common.Utils;

using Autofac;
Expand Down Expand Up @@ -92,8 +91,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
services.AddMemoryCache();
var diHelper = new DIHelper(services);

diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
LogNLogExtension.ConfigureLog(diHelper, "ASC.Thumbnails.Svc");
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
services.AddHostedService<ThumbnailsServiceLauncher>();
diHelper.TryAdd<ThumbnailsServiceLauncher>();
})
Expand Down
4 changes: 1 addition & 3 deletions common/services/ASC.UrlShortener.Svc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
using ASC.Api.Core;
using ASC.Common;
using ASC.Common.DependencyInjection;
using ASC.Common.Logging;
using ASC.Common.Utils;

using Autofac;
Expand Down Expand Up @@ -93,8 +92,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
services.AddMemoryCache();

var diHelper = new DIHelper(services);

LogNLogExtension.ConfigureLog(diHelper, "ASC.UrlShortener.Svc");

services.AddHostedService<UrlShortenerServiceLauncher>();
diHelper.TryAdd<UrlShortenerServiceLauncher>();
})
Expand Down
15 changes: 5 additions & 10 deletions products/ASC.CRM/BackgroundTasks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;


using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.Logging;
using ASC.Common.DependencyInjection;
using ASC.Common.Utils;
using ASC.ElasticSearch;
using ASC.Web.CRM.Core.Search;


using Autofac;
using Autofac.Extensions.DependencyInjection;


using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ASC.Common.DependencyInjection;


namespace ASC.CRM.BackgroundTasks
Expand Down Expand Up @@ -73,9 +71,6 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>

services.AddHostedService<ServiceLauncher>();
diHelper.TryAdd<ServiceLauncher>();

LogNLogExtension.ConfigureLog(diHelper, "ASC.Files", "ASC.Feed.Agregator");

diHelper.TryAdd<FactoryIndexerCase>();
diHelper.TryAdd<FactoryIndexerContact>();
diHelper.TryAdd<FactoryIndexerContactInfo>();
Expand Down
2 changes: 0 additions & 2 deletions products/ASC.Calendar/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace ASC.Calendar
{
public class Startup : BaseStartup
{
public override string[] LogParams { get => new string[] { "ASC.Calendar" }; }

public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
: base(configuration, hostEnvironment)
{
Expand Down
3 changes: 1 addition & 2 deletions products/ASC.Files/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
namespace ASC.Files
{
public class Startup : BaseStartup
{
public override string[] LogParams { get => new string[] { "ASC.Files" }; }
{
public override JsonConverter[] Converters { get => new JsonConverter[] { new FileEntryWrapperConverter() }; }

public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
Expand Down
Loading

0 comments on commit ad57fee

Please sign in to comment.