logger refactoring. added resolver ILog/IOptionsMonitor<ILog>

This commit is contained in:
Alexey Bannov 2022-03-17 09:55:08 +03:00
parent 8ee71f5574
commit 21f85e7260
12 changed files with 1104 additions and 1282 deletions

View File

@ -176,13 +176,38 @@ public abstract class BaseStartup
public static class LogNLogConfigureExtenstion
{
private static LoggingConfiguration GetXmlLoggingConfiguration(IHostEnvironment hostEnvironment,IConfiguration configuration)
{
var loggerConfiguration = new XmlLoggingConfiguration(CrossPlatform.PathCombine(configuration["pathToConf"], "nlog.config"));
var settings = new ConfigurationExtension(configuration).GetSetting<NLogSettings>("log");
if (!string.IsNullOrEmpty(settings.Name))
{
loggerConfiguration.Variables["name"] = settings.Name;
}
if (!string.IsNullOrEmpty(settings.Dir))
{
loggerConfiguration.Variables["dir"] = CrossPlatform.PathCombine(hostEnvironment.ContentRootPath, settings.Dir)
.TrimEnd('/').TrimEnd('\\') + Path.DirectorySeparatorChar;
}
return loggerConfiguration;
}
public static IHostBuilder ConfigureNLogLogging(this IHostBuilder hostBuilder)
{
return hostBuilder.ConfigureLogging((hostBuildexContext, r) =>
{
_ = new ConfigureLogNLog(hostBuildexContext.Configuration,
new ConfigurationExtension(hostBuildexContext.Configuration), hostBuildexContext.HostingEnvironment);
r.AddNLog(LogManager.Configuration);
r.Services.ConfigureOptions<ConfigureLogNLog>();
r.Services.AddSingleton(resolver => (IOptionsMonitor<ILog>)resolver.GetRequiredService<IOptionsMonitor<LogNLog>>());
r.Services.AddSingleton(resolver => (ILog)resolver.GetRequiredService<IOptionsMonitor<LogNLog>>().CurrentValue);
LogManager.ThrowConfigExceptions = false;
r.AddNLog(GetXmlLoggingConfiguration(hostBuildexContext.HostingEnvironment, hostBuildexContext.Configuration));
});
}
}

View File

@ -1,9 +1,7 @@
global using System.ComponentModel;
global using System.Globalization;
global using System.Linq.Expressions;
global using System.Net;
global using System.Reflection;
global using System.Runtime.Serialization;
global using System.Security;
@ -12,10 +10,9 @@ global using System.Security.Claims;
global using System.Text.Encodings.Web;
global using System.Text.Json;
global using System.Text.Json.Serialization;
global using System.Web;
global using System.Xml.Linq;
global using ASC.Api.Core;
global using ASC.Api.Core.Auth;
global using ASC.Api.Core.Convention;
@ -40,13 +37,13 @@ global using ASC.Web.Core.Helpers;
global using ASC.Web.Core.Users;
global using ASC.Web.Studio.Utility;
global using ASC.Webhooks.Core;
global using Autofac;
global using Confluent.Kafka;
global using HealthChecks.UI.Client;
global using Microsoft.AspNetCore.Authentication;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Builder;
@ -70,12 +67,13 @@ global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using Microsoft.Extensions.Primitives;
global using Newtonsoft.Json;
global using Newtonsoft.Json.Serialization;
global using NLog;
global using NLog.Config;
global using NLog.Extensions.Logging;
global using StackExchange.Redis.Extensions.Core.Configuration;
global using StackExchange.Redis.Extensions.Newtonsoft;
global using StackExchange.Redis.Extensions.Newtonsoft;

View File

@ -0,0 +1,86 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Common.Logging;
public interface ILog
{
bool IsDebugEnabled { get; }
bool IsInfoEnabled { get; }
bool IsWarnEnabled { get; }
bool IsErrorEnabled { get; }
bool IsFatalEnabled { get; }
bool IsTraceEnabled { get; }
void Trace(object message);
void TraceFormat(string message, object arg0);
void DebugWithProps(string message, IEnumerable<KeyValuePair<string, object>> props);
void Debug(object message);
void Debug(object message, Exception exception);
void DebugFormat(string format, params object[] args);
void DebugFormat(string format, object arg0);
void DebugFormat(string format, object arg0, object arg1);
void DebugFormat(string format, object arg0, object arg1, object arg2);
void DebugFormat(IFormatProvider provider, string format, params object[] args);
void Info(object message);
void Info(string message, Exception exception);
void InfoFormat(string format, params object[] args);
void InfoFormat(string format, object arg0);
void InfoFormat(string format, object arg0, object arg1);
void InfoFormat(string format, object arg0, object arg1, object arg2);
void InfoFormat(IFormatProvider provider, string format, params object[] args);
void Warn(object message);
void Warn(object message, Exception exception);
void WarnFormat(string format, params object[] args);
void WarnFormat(string format, object arg0);
void WarnFormat(string format, object arg0, object arg1);
void WarnFormat(string format, object arg0, object arg1, object arg2);
void WarnFormat(IFormatProvider provider, string format, params object[] args);
void Error(object message);
void Error(object message, Exception exception);
void ErrorFormat(string format, params object[] args);
void ErrorFormat(string format, object arg0);
void ErrorFormat(string format, object arg0, object arg1);
void ErrorFormat(string format, object arg0, object arg1, object arg2);
void ErrorFormat(IFormatProvider provider, string format, params object[] args);
void Fatal(object message);
void Fatal(string message, Exception exception);
void FatalFormat(string format, params object[] args);
void FatalFormat(string format, object arg0);
void FatalFormat(string format, object arg0, object arg1);
void FatalFormat(string format, object arg0, object arg1, object arg2);
void FatalFormat(IFormatProvider provider, string format, params object[] args);
string LogDirectory { get; }
string Name { get; set; }
void Configure(string name);
}

View File

@ -1,999 +0,0 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using LogLevel = NLog.LogLevel;
namespace ASC.Common.Logging;
[Singletone(typeof(ConfigureLogNLog), Additional = typeof(LogNLogExtension))]
public interface ILog
{
bool IsDebugEnabled { get; }
bool IsInfoEnabled { get; }
bool IsWarnEnabled { get; }
bool IsErrorEnabled { get; }
bool IsFatalEnabled { get; }
bool IsTraceEnabled { get; }
void Trace(object message);
void TraceFormat(string message, object arg0);
void DebugWithProps(string message, IEnumerable<KeyValuePair<string, object>> props);
void Debug(object message);
void Debug(object message, Exception exception);
void DebugFormat(string format, params object[] args);
void DebugFormat(string format, object arg0);
void DebugFormat(string format, object arg0, object arg1);
void DebugFormat(string format, object arg0, object arg1, object arg2);
void DebugFormat(IFormatProvider provider, string format, params object[] args);
void Info(object message);
void Info(string message, Exception exception);
void InfoFormat(string format, params object[] args);
void InfoFormat(string format, object arg0);
void InfoFormat(string format, object arg0, object arg1);
void InfoFormat(string format, object arg0, object arg1, object arg2);
void InfoFormat(IFormatProvider provider, string format, params object[] args);
void Warn(object message);
void Warn(object message, Exception exception);
void WarnFormat(string format, params object[] args);
void WarnFormat(string format, object arg0);
void WarnFormat(string format, object arg0, object arg1);
void WarnFormat(string format, object arg0, object arg1, object arg2);
void WarnFormat(IFormatProvider provider, string format, params object[] args);
void Error(object message);
void Error(object message, Exception exception);
void ErrorFormat(string format, params object[] args);
void ErrorFormat(string format, object arg0);
void ErrorFormat(string format, object arg0, object arg1);
void ErrorFormat(string format, object arg0, object arg1, object arg2);
void ErrorFormat(IFormatProvider provider, string format, params object[] args);
void Fatal(object message);
void Fatal(string message, Exception exception);
void FatalFormat(string format, params object[] args);
void FatalFormat(string format, object arg0);
void FatalFormat(string format, object arg0, object arg1);
void FatalFormat(string format, object arg0, object arg1, object arg2);
void FatalFormat(IFormatProvider provider, string format, params object[] args);
string LogDirectory { get; }
string Name { get; set; }
void Configure(string name);
}
public class Log : ILog
{
public string Name { get; set; }
public bool IsDebugEnabled { get; private set; }
public bool IsInfoEnabled { get; private set; }
public bool IsWarnEnabled { get; private set; }
public bool IsErrorEnabled { get; private set; }
public bool IsFatalEnabled { get; private set; }
public bool IsTraceEnabled { get; private set; }
public string LogDirectory => log4net.GlobalContext.Properties["LogDirectory"].ToString();
private log4net.ILog _logger;
public Log(string name)
{
Configure(name);
}
static Log()
{
XmlConfigurator.Configure(log4net.LogManager.GetRepository(Assembly.GetCallingAssembly()));
}
public void Configure(string name)
{
_logger = log4net.LogManager.GetLogger(Assembly.GetCallingAssembly(), name);
IsDebugEnabled = _logger.IsDebugEnabled;
IsInfoEnabled = _logger.IsInfoEnabled;
IsWarnEnabled = _logger.IsWarnEnabled;
IsErrorEnabled = _logger.IsErrorEnabled;
IsFatalEnabled = _logger.IsFatalEnabled;
IsTraceEnabled = _logger.Logger.IsEnabledFor(Level.Trace);
}
public void Trace(object message)
{
if (IsTraceEnabled)
{
_logger.Logger.Log(GetType(), Level.Trace, message, null);
}
}
public void TraceFormat(string message, object arg0)
{
if (IsTraceEnabled)
{
_logger.Logger.Log(GetType(), Level.Trace, string.Format(message, arg0), null);
}
}
public void Debug(object message)
{
if (IsDebugEnabled)
{
_logger.Debug(message);
}
}
public void Debug(object message, Exception exception)
{
if (IsDebugEnabled)
{
_logger.Debug(message, exception);
}
}
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(format, args);
}
}
public void DebugFormat(string format, object arg0)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(format, arg0);
}
}
public void DebugFormat(string format, object arg0, object arg1)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(format, arg0, arg1);
}
}
public void DebugFormat(string format, object arg0, object arg1, object arg2)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(format, arg0, arg1, arg2);
}
}
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(provider, format, args);
}
}
public void DebugWithProps(string message, IEnumerable<KeyValuePair<string, object>> props)
{
if (!IsDebugEnabled)
{
return;
}
foreach (var p in props)
{
log4net.ThreadContext.Properties[p.Key] = p.Value;
}
_logger.Debug(message);
}
public void Info(object message)
{
if (IsInfoEnabled)
{
_logger.Info(message);
}
}
public void Info(string message, Exception exception)
{
if (IsInfoEnabled)
{
_logger.Info(message, exception);
}
}
public void InfoFormat(string format, params object[] args)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(format, args);
}
}
public void InfoFormat(string format, object arg0)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(format, arg0);
}
}
public void InfoFormat(string format, object arg0, object arg1)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(format, arg0, arg1);
}
}
public void InfoFormat(string format, object arg0, object arg1, object arg2)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(format, arg0, arg1, arg2);
}
}
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(provider, format, args);
}
}
public void Warn(object message)
{
if (IsWarnEnabled)
{
_logger.Warn(message);
}
}
public void Warn(object message, Exception exception)
{
if (IsWarnEnabled)
{
_logger.Warn(message, exception);
}
}
public void WarnFormat(string format, params object[] args)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(format, args);
}
}
public void WarnFormat(string format, object arg0)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(format, arg0);
}
}
public void WarnFormat(string format, object arg0, object arg1)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(format, arg0, arg1);
}
}
public void WarnFormat(string format, object arg0, object arg1, object arg2)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(format, arg0, arg1, arg2);
}
}
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(provider, format, args);
}
}
public void Error(object message)
{
if (IsErrorEnabled)
{
_logger.Error(message);
}
}
public void Error(object message, Exception exception)
{
if (IsErrorEnabled)
{
_logger.Error(message, exception);
}
}
public void ErrorFormat(string format, params object[] args)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(format, args);
}
}
public void ErrorFormat(string format, object arg0)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(format, arg0);
}
}
public void ErrorFormat(string format, object arg0, object arg1)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(format, arg0, arg1);
}
}
public void ErrorFormat(string format, object arg0, object arg1, object arg2)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(format, arg0, arg1, arg2);
}
}
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(provider, format, args);
}
}
public void Fatal(object message)
{
if (IsFatalEnabled)
{
_logger.Fatal(message);
}
}
public void Fatal(string message, Exception exception)
{
if (IsFatalEnabled)
{
_logger.Fatal(message, exception);
}
}
public void FatalFormat(string format, params object[] args)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(format, args);
}
}
public void FatalFormat(string format, object arg0)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(format, arg0);
}
}
public void FatalFormat(string format, object arg0, object arg1)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(format, arg0, arg1);
}
}
public void FatalFormat(string format, object arg0, object arg1, object arg2)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(format, arg0, arg1, arg2);
}
}
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(provider, format, args);
}
}
public void DebugWithProps(string message, KeyValuePair<string, object> prop1, KeyValuePair<string, object> prop2, KeyValuePair<string, object> prop3)
{
if (!IsDebugEnabled)
{
return;
}
AddProp(prop1);
AddProp(prop2);
AddProp(prop3);
_logger.Debug(message);
static void AddProp(KeyValuePair<string, object> p)
{
log4net.ThreadContext.Properties[p.Key] = p.Value;
}
}
}
public class NLogSettings
{
public string Name { get; set; }
public string Dir { get; set; }
}
[Singletone]
public class ConfigureLogNLog : IConfigureNamedOptions<LogNLog>
{
private readonly IConfiguration _configuration;
private readonly ConfigurationExtension _configurationExtension;
public ConfigureLogNLog(IConfiguration configuration, ConfigurationExtension configurationExtension, IHostEnvironment hostingEnvironment)
{
_configuration = configuration;
_configurationExtension = configurationExtension;
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(CrossPlatform.PathCombine(_configuration["pathToConf"], "nlog.config"));
LogManager.ThrowConfigExceptions = false;
var settings = _configurationExtension.GetSetting<NLogSettings>("log");
if (!string.IsNullOrEmpty(settings.Name))
{
LogManager.Configuration.Variables["name"] = settings.Name;
}
if (!string.IsNullOrEmpty(settings.Dir))
{
LogManager.Configuration.Variables["dir"] = CrossPlatform.PathCombine(hostingEnvironment.ContentRootPath, settings.Dir)
.TrimEnd('/').TrimEnd('\\') + Path.DirectorySeparatorChar;
}
Target.Register<SelfCleaningTarget>("SelfCleaning");
}
public void Configure(LogNLog options) => options.Configure("ASC");
public void Configure(string name, LogNLog options)
{
if (string.IsNullOrEmpty(name))
{
Configure(options);
}
else
{
options.Configure(name);
}
}
}
public class LogNLog : ILog
{
public bool IsDebugEnabled { get; private set; }
public bool IsInfoEnabled { get; private set; }
public bool IsWarnEnabled { get; private set; }
public bool IsErrorEnabled { get; private set; }
public bool IsFatalEnabled { get; private set; }
public bool IsTraceEnabled { get; private set; }
public string LogDirectory => LogManager.Configuration.Variables["dir"].Text;
public string Name
{
get => _name;
set
{
_name = value;
Logger = LogManager.GetLogger(_name);
}
}
private NLog.ILogger Logger
{
get => _logger;
set
{
_logger = value;
IsDebugEnabled = _logger.IsDebugEnabled;
IsInfoEnabled = _logger.IsInfoEnabled;
IsWarnEnabled = _logger.IsWarnEnabled;
IsErrorEnabled = _logger.IsErrorEnabled;
IsFatalEnabled = _logger.IsFatalEnabled;
IsTraceEnabled = _logger.IsEnabled(LogLevel.Trace);
}
}
private NLog.ILogger _logger;
private string _name;
public void Configure(string name)
{
Name = name;
}
public void Trace(object message)
{
if (IsTraceEnabled)
{
Logger.Log(LogLevel.Trace, message);
}
}
public void TraceFormat(string message, object arg0)
{
if (IsTraceEnabled)
{
Logger.Log(LogLevel.Trace, string.Format(message, arg0));
}
}
public void Debug(object message)
{
if (IsDebugEnabled)
{
Logger.Debug(message);
}
}
public void Debug(object message, Exception exception)
{
if (IsDebugEnabled)
{
Logger.Debug(exception, "{0}", message);
}
}
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
{
Logger.Debug(format, args);
}
}
public void DebugFormat(string format, object arg0)
{
if (IsDebugEnabled)
{
Logger.Debug(format, arg0);
}
}
public void DebugFormat(string format, object arg0, object arg1)
{
if (IsDebugEnabled)
{
Logger.Debug(format, arg0, arg1);
}
}
public void DebugFormat(string format, object arg0, object arg1, object arg2)
{
if (IsDebugEnabled)
{
Logger.Debug(format, arg0, arg1, arg2);
}
}
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsDebugEnabled)
{
Logger.Debug(provider, format, args);
}
}
public void DebugWithProps(string message, IEnumerable<KeyValuePair<string, object>> props)
{
if (!IsDebugEnabled)
{
return;
}
var theEvent = new LogEventInfo { Message = message, LoggerName = Name, Level = LogLevel.Debug };
foreach (var p in props)
{
theEvent.Properties[p.Key] = p.Value;
}
Logger.Log(theEvent);
}
public void Info(object message)
{
if (IsInfoEnabled)
{
Logger.Info(message);
}
}
public void Info(string message, Exception exception)
{
if (IsInfoEnabled)
{
Logger.Info(exception, message);
}
}
public void InfoFormat(string format, params object[] args)
{
if (IsInfoEnabled)
{
Logger.Info(format, args);
}
}
public void InfoFormat(string format, object arg0)
{
if (IsInfoEnabled)
{
Logger.Info(format, arg0);
}
}
public void InfoFormat(string format, object arg0, object arg1)
{
if (IsInfoEnabled)
{
Logger.Info(format, arg0, arg1);
}
}
public void InfoFormat(string format, object arg0, object arg1, object arg2)
{
if (IsInfoEnabled)
{
Logger.Info(format, arg0, arg1, arg2);
}
}
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsInfoEnabled)
{
Logger.Info(provider, format, args);
}
}
public void Warn(object message)
{
if (IsWarnEnabled)
{
Logger.Warn(message);
}
}
public void Warn(object message, Exception exception)
{
if (IsWarnEnabled)
{
Logger.Warn(exception, "{0}", message);
}
}
public void WarnFormat(string format, params object[] args)
{
if (IsWarnEnabled)
{
Logger.Warn(format, args);
}
}
public void WarnFormat(string format, object arg0)
{
if (IsWarnEnabled)
{
Logger.Warn(format, arg0);
}
}
public void WarnFormat(string format, object arg0, object arg1)
{
if (IsWarnEnabled)
{
Logger.Warn(format, arg0, arg1);
}
}
public void WarnFormat(string format, object arg0, object arg1, object arg2)
{
if (IsWarnEnabled)
{
Logger.Warn(format, arg0, arg1, arg2);
}
}
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsWarnEnabled)
{
Logger.Warn(provider, format, args);
}
}
public void Error(object message)
{
if (IsErrorEnabled)
{
Logger.Error(message);
}
}
public void Error(object message, Exception exception)
{
if (IsErrorEnabled)
{
Logger.Error(exception, "{0}", message);
}
}
public void ErrorFormat(string format, params object[] args)
{
if (IsErrorEnabled)
{
Logger.Error(format, args);
}
}
public void ErrorFormat(string format, object arg0)
{
if (IsErrorEnabled)
{
Logger.Error(format, arg0);
}
}
public void ErrorFormat(string format, object arg0, object arg1)
{
if (IsErrorEnabled)
{
Logger.Error(format, arg0, arg1);
}
}
public void ErrorFormat(string format, object arg0, object arg1, object arg2)
{
if (IsErrorEnabled)
{
Logger.Error(format, arg0, arg1, arg2);
}
}
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsErrorEnabled)
{
Logger.Error(provider, format, args);
}
}
public void Fatal(object message)
{
if (IsFatalEnabled)
{
Logger.Fatal(message);
}
}
public void Fatal(string message, Exception exception)
{
if (IsFatalEnabled)
{
Logger.Fatal(exception, message);
}
}
public void FatalFormat(string format, params object[] args)
{
if (IsFatalEnabled)
{
Logger.Fatal(format, args);
}
}
public void FatalFormat(string format, object arg0)
{
if (IsFatalEnabled)
{
Logger.Fatal(format, arg0);
}
}
public void FatalFormat(string format, object arg0, object arg1)
{
if (IsFatalEnabled)
{
Logger.Fatal(format, arg0, arg1);
}
}
public void FatalFormat(string format, object arg0, object arg1, object arg2)
{
if (IsFatalEnabled)
{
Logger.Fatal(format, arg0, arg1, arg2);
}
}
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsFatalEnabled)
{
Logger.Fatal(provider, format, args);
}
}
}
public class NullLog : ILog
{
public bool IsDebugEnabled { get; set; }
public bool IsInfoEnabled { get; set; }
public bool IsWarnEnabled { get; set; }
public bool IsErrorEnabled { get; set; }
public bool IsFatalEnabled { get; set; }
public bool IsTraceEnabled { get; set; }
public string Name { get; set; }
public string LogDirectory => string.Empty;
public void Trace(object message) { }
public void TraceFormat(string message, object arg0) { }
public void DebugWithProps(string message, IEnumerable<KeyValuePair<string, object>> props) { }
public void Debug(object message) { }
public void Debug(object message, Exception exception) { }
public void DebugFormat(string format, params object[] args) { }
public void DebugFormat(string format, object arg0) { }
public void DebugFormat(string format, object arg0, object arg1) { }
public void DebugFormat(string format, object arg0, object arg1, object arg2) { }
public void DebugFormat(IFormatProvider provider, string format, params object[] args) { }
public void Info(object message) { }
public void Info(string message, Exception exception) { }
public void InfoFormat(string format, params object[] args) { }
public void InfoFormat(string format, object arg0) { }
public void InfoFormat(string format, object arg0, object arg1) { }
public void InfoFormat(string format, object arg0, object arg1, object arg2) { }
public void InfoFormat(IFormatProvider provider, string format, params object[] args) { }
public void Warn(object message) { }
public void Warn(object message, Exception exception) { }
public void WarnFormat(string format, params object[] args) { }
public void WarnFormat(string format, object arg0) { }
public void WarnFormat(string format, object arg0, object arg1) { }
public void WarnFormat(string format, object arg0, object arg1, object arg2) { }
public void WarnFormat(IFormatProvider provider, string format, params object[] args) { }
public void Error(object message) { }
public void Error(object message, Exception exception) { }
public void ErrorFormat(string format, params object[] args) { }
public void ErrorFormat(string format, object arg0) { }
public void ErrorFormat(string format, object arg0, object arg1) { }
public void ErrorFormat(string format, object arg0, object arg1, object arg2) { }
public void ErrorFormat(IFormatProvider provider, string format, params object[] args) { }
public void Fatal(object message) { }
public void Fatal(string message, Exception exception) { }
public void FatalFormat(string format, params object[] args) { }
public void FatalFormat(string format, object arg0) { }
public void FatalFormat(string format, object arg0, object arg1) { }
public void FatalFormat(string format, object arg0, object arg1, object arg2) { }
public void FatalFormat(IFormatProvider provider, string format, params object[] args) { }
public void Configure(string name) { }
}
[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)
{
var log = base.Get(name);
if (string.IsNullOrEmpty(log?.Name))
{
log = CurrentValue;
}
return log;
}
}
public static class LoggerExtension<T> where T : class, ILog, new()
{
public static void RegisterLog(DIHelper services)
{
services.TryAdd(typeof(IOptionsMonitor<ILog>), typeof(LogManager<T>));
}
}
public static class LogNLogExtension
{
public static void Register(DIHelper services)
{
LoggerExtension<LogNLog>.RegisterLog(services);
}
}

View File

@ -0,0 +1,47 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Common.Logging;
public class ConfigureLogNLog : IConfigureNamedOptions<LogNLog>
{
public ConfigureLogNLog()
{
}
public void Configure(LogNLog options) => options.Configure("ASC");
public void Configure(string name, LogNLog options)
{
if (string.IsNullOrEmpty(name))
{
Configure(options);
}
else
{
options.Configure(name);
}
}
}

View File

@ -0,0 +1,389 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using LogLevel = NLog.LogLevel;
namespace ASC.Common.Logging;
public class LogNLog : ILog
{
public bool IsDebugEnabled { get; private set; }
public bool IsInfoEnabled { get; private set; }
public bool IsWarnEnabled { get; private set; }
public bool IsErrorEnabled { get; private set; }
public bool IsFatalEnabled { get; private set; }
public bool IsTraceEnabled { get; private set; }
public string LogDirectory => LogManager.Configuration.Variables["dir"].Text;
public string Name
{
get => _name;
set
{
_name = value;
Logger = LogManager.GetLogger(_name);
}
}
private NLog.ILogger Logger
{
get => _logger;
set
{
_logger = value;
IsDebugEnabled = _logger.IsDebugEnabled;
IsInfoEnabled = _logger.IsInfoEnabled;
IsWarnEnabled = _logger.IsWarnEnabled;
IsErrorEnabled = _logger.IsErrorEnabled;
IsFatalEnabled = _logger.IsFatalEnabled;
IsTraceEnabled = _logger.IsEnabled(LogLevel.Trace);
}
}
private NLog.ILogger _logger;
private string _name;
public void Configure(string name)
{
Name = name;
}
public void Trace(object message)
{
if (IsTraceEnabled)
{
Logger.Log(LogLevel.Trace, message);
}
}
public void TraceFormat(string message, object arg0)
{
if (IsTraceEnabled)
{
Logger.Log(LogLevel.Trace, string.Format(message, arg0));
}
}
public void Debug(object message)
{
if (IsDebugEnabled)
{
Logger.Debug(message);
}
}
public void Debug(object message, Exception exception)
{
if (IsDebugEnabled)
{
Logger.Debug(exception, "{0}", message);
}
}
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
{
Logger.Debug(format, args);
}
}
public void DebugFormat(string format, object arg0)
{
if (IsDebugEnabled)
{
Logger.Debug(format, arg0);
}
}
public void DebugFormat(string format, object arg0, object arg1)
{
if (IsDebugEnabled)
{
Logger.Debug(format, arg0, arg1);
}
}
public void DebugFormat(string format, object arg0, object arg1, object arg2)
{
if (IsDebugEnabled)
{
Logger.Debug(format, arg0, arg1, arg2);
}
}
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsDebugEnabled)
{
Logger.Debug(provider, format, args);
}
}
public void DebugWithProps(string message, IEnumerable<KeyValuePair<string, object>> props)
{
if (!IsDebugEnabled)
{
return;
}
var theEvent = new LogEventInfo { Message = message, LoggerName = Name, Level = LogLevel.Debug };
foreach (var p in props)
{
theEvent.Properties[p.Key] = p.Value;
}
Logger.Log(theEvent);
}
public void Info(object message)
{
if (IsInfoEnabled)
{
Logger.Info(message);
}
}
public void Info(string message, Exception exception)
{
if (IsInfoEnabled)
{
Logger.Info(exception, message);
}
}
public void InfoFormat(string format, params object[] args)
{
if (IsInfoEnabled)
{
Logger.Info(format, args);
}
}
public void InfoFormat(string format, object arg0)
{
if (IsInfoEnabled)
{
Logger.Info(format, arg0);
}
}
public void InfoFormat(string format, object arg0, object arg1)
{
if (IsInfoEnabled)
{
Logger.Info(format, arg0, arg1);
}
}
public void InfoFormat(string format, object arg0, object arg1, object arg2)
{
if (IsInfoEnabled)
{
Logger.Info(format, arg0, arg1, arg2);
}
}
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsInfoEnabled)
{
Logger.Info(provider, format, args);
}
}
public void Warn(object message)
{
if (IsWarnEnabled)
{
Logger.Warn(message);
}
}
public void Warn(object message, Exception exception)
{
if (IsWarnEnabled)
{
Logger.Warn(exception, "{0}", message);
}
}
public void WarnFormat(string format, params object[] args)
{
if (IsWarnEnabled)
{
Logger.Warn(format, args);
}
}
public void WarnFormat(string format, object arg0)
{
if (IsWarnEnabled)
{
Logger.Warn(format, arg0);
}
}
public void WarnFormat(string format, object arg0, object arg1)
{
if (IsWarnEnabled)
{
Logger.Warn(format, arg0, arg1);
}
}
public void WarnFormat(string format, object arg0, object arg1, object arg2)
{
if (IsWarnEnabled)
{
Logger.Warn(format, arg0, arg1, arg2);
}
}
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsWarnEnabled)
{
Logger.Warn(provider, format, args);
}
}
public void Error(object message)
{
if (IsErrorEnabled)
{
Logger.Error(message);
}
}
public void Error(object message, Exception exception)
{
if (IsErrorEnabled)
{
Logger.Error(exception, "{0}", message);
}
}
public void ErrorFormat(string format, params object[] args)
{
if (IsErrorEnabled)
{
Logger.Error(format, args);
}
}
public void ErrorFormat(string format, object arg0)
{
if (IsErrorEnabled)
{
Logger.Error(format, arg0);
}
}
public void ErrorFormat(string format, object arg0, object arg1)
{
if (IsErrorEnabled)
{
Logger.Error(format, arg0, arg1);
}
}
public void ErrorFormat(string format, object arg0, object arg1, object arg2)
{
if (IsErrorEnabled)
{
Logger.Error(format, arg0, arg1, arg2);
}
}
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsErrorEnabled)
{
Logger.Error(provider, format, args);
}
}
public void Fatal(object message)
{
if (IsFatalEnabled)
{
Logger.Fatal(message);
}
}
public void Fatal(string message, Exception exception)
{
if (IsFatalEnabled)
{
Logger.Fatal(exception, message);
}
}
public void FatalFormat(string format, params object[] args)
{
if (IsFatalEnabled)
{
Logger.Fatal(format, args);
}
}
public void FatalFormat(string format, object arg0)
{
if (IsFatalEnabled)
{
Logger.Fatal(format, arg0);
}
}
public void FatalFormat(string format, object arg0, object arg1)
{
if (IsFatalEnabled)
{
Logger.Fatal(format, arg0, arg1);
}
}
public void FatalFormat(string format, object arg0, object arg1, object arg2)
{
if (IsFatalEnabled)
{
Logger.Fatal(format, arg0, arg1, arg2);
}
}
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsFatalEnabled)
{
Logger.Fatal(provider, format, args);
}
}
}

View File

@ -0,0 +1,32 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Common.Logging;
public class NLogSettings
{
public string Name { get; set; }
public string Dir { get; set; }
}

View File

@ -0,0 +1,116 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Common.Logging;
public class NullLog : ILog
{
public bool IsDebugEnabled { get; set; }
public bool IsInfoEnabled { get; set; }
public bool IsWarnEnabled { get; set; }
public bool IsErrorEnabled { get; set; }
public bool IsFatalEnabled { get; set; }
public bool IsTraceEnabled { get; set; }
public string Name { get; set; }
public string LogDirectory => string.Empty;
public void Trace(object message) { }
public void TraceFormat(string message, object arg0) { }
public void DebugWithProps(string message, IEnumerable<KeyValuePair<string, object>> props) { }
public void Debug(object message) { }
public void Debug(object message, Exception exception) { }
public void DebugFormat(string format, params object[] args) { }
public void DebugFormat(string format, object arg0) { }
public void DebugFormat(string format, object arg0, object arg1) { }
public void DebugFormat(string format, object arg0, object arg1, object arg2) { }
public void DebugFormat(IFormatProvider provider, string format, params object[] args) { }
public void Info(object message) { }
public void Info(string message, Exception exception) { }
public void InfoFormat(string format, params object[] args) { }
public void InfoFormat(string format, object arg0) { }
public void InfoFormat(string format, object arg0, object arg1) { }
public void InfoFormat(string format, object arg0, object arg1, object arg2) { }
public void InfoFormat(IFormatProvider provider, string format, params object[] args) { }
public void Warn(object message) { }
public void Warn(object message, Exception exception) { }
public void WarnFormat(string format, params object[] args) { }
public void WarnFormat(string format, object arg0) { }
public void WarnFormat(string format, object arg0, object arg1) { }
public void WarnFormat(string format, object arg0, object arg1, object arg2) { }
public void WarnFormat(IFormatProvider provider, string format, params object[] args) { }
public void Error(object message) { }
public void Error(object message, Exception exception) { }
public void ErrorFormat(string format, params object[] args) { }
public void ErrorFormat(string format, object arg0) { }
public void ErrorFormat(string format, object arg0, object arg1) { }
public void ErrorFormat(string format, object arg0, object arg1, object arg2) { }
public void ErrorFormat(IFormatProvider provider, string format, params object[] args) { }
public void Fatal(object message) { }
public void Fatal(string message, Exception exception) { }
public void FatalFormat(string format, params object[] args) { }
public void FatalFormat(string format, object arg0) { }
public void FatalFormat(string format, object arg0, object arg1) { }
public void FatalFormat(string format, object arg0, object arg1, object arg2) { }
public void FatalFormat(IFormatProvider provider, string format, params object[] args) { }
public void Configure(string name) { }
}

View File

@ -1,111 +0,0 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Common.Logging;
public class SelfCleaningAppender : RollingFileAppender
{
private static DateTime _lastCleanDate;
private static int? _cleanPeriod;
protected override void Append(LoggingEvent loggingEvent)
{
if (DateTime.UtcNow.Date > _lastCleanDate.Date)
{
_lastCleanDate = DateTime.UtcNow.Date;
Clean();
}
base.Append(loggingEvent);
}
protected override void Append(LoggingEvent[] loggingEvents)
{
if (DateTime.UtcNow.Date > _lastCleanDate.Date)
{
_lastCleanDate = DateTime.UtcNow.Date;
Clean();
}
base.Append(loggingEvents);
}
private static int GetCleanPeriod()
{
if (_cleanPeriod != null)
{
return _cleanPeriod.Value;
}
const string key = "CleanPeriod";
var value = 30;
var repo = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
if (repo != null && repo.Properties.GetKeys().Contains(key))
{
int.TryParse(repo.Properties[key].ToString(), out value);
}
_cleanPeriod = value;
return value;
}
private void Clean()
{
try
{
if (string.IsNullOrEmpty(File))
{
return;
}
var fileInfo = new FileInfo(File);
if (!fileInfo.Exists)
{
return;
}
var directory = fileInfo.Directory;
if (directory == null || !directory.Exists)
{
return;
}
var files = directory.GetFiles();
var cleanPeriod = GetCleanPeriod();
foreach (var file in files.Where(file => (DateTime.UtcNow.Date - file.CreationTimeUtc.Date).Days > cleanPeriod))
{
file.Delete();
}
}
catch (Exception err)
{
LogLog.Error(GetType(), err.Message, err);
}
}
}

View File

@ -1,149 +0,0 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using LogLevel = NLog.LogLevel;
namespace ASC.Common.Logging;
[Target("SelfCleaning")]
public class SelfCleaningTarget : FileTarget
{
private static DateTime _lastCleanDate;
private static int? _cleanPeriod;
protected override void Write(IList<AsyncLogEventInfo> logEvents)
{
if (DateTime.UtcNow.Date > _lastCleanDate.Date)
{
_lastCleanDate = DateTime.UtcNow.Date;
Clean();
}
var buffer = new List<AsyncLogEventInfo>();
foreach (var logEvent in logEvents)
{
buffer.Add(logEvent);
if (buffer.Count < 10)
{
continue;
}
base.Write(buffer);
buffer.Clear();
}
base.Write(buffer);
}
protected override void Write(LogEventInfo logEvent)
{
if (DateTime.UtcNow.Date > _lastCleanDate.Date)
{
_lastCleanDate = DateTime.UtcNow.Date;
Clean();
}
base.Write(logEvent);
}
private static int GetCleanPeriod()
{
if (_cleanPeriod != null)
{
return _cleanPeriod.Value;
}
var value = 30;
const string key = "cleanPeriod";
if (LogManager.Configuration.Variables.TryGetValue(key, out var variable))
{
if (variable != null && !string.IsNullOrEmpty(variable.Text))
{
int.TryParse(variable.Text, out value);
}
}
_cleanPeriod = value;
return value;
}
private void Clean()
{
var filePath = string.Empty;
var dirPath = string.Empty;
try
{
if (FileName == null)
{
return;
}
filePath = ((NLog.Layouts.SimpleLayout)FileName).Text;
if (string.IsNullOrEmpty(filePath))
{
return;
}
dirPath = Path.GetDirectoryName(filePath);
if (string.IsNullOrEmpty(dirPath))
{
return;
}
if (!Path.IsPathRooted(dirPath))
{
dirPath = CrossPlatform.PathCombine(AppDomain.CurrentDomain.BaseDirectory, dirPath);
}
var directory = new DirectoryInfo(dirPath);
if (!directory.Exists)
{
return;
}
var files = directory.GetFiles();
var cleanPeriod = GetCleanPeriod();
foreach (var file in files.Where(file => (DateTime.UtcNow.Date - file.CreationTimeUtc.Date).Days > cleanPeriod))
{
file.Delete();
}
}
catch (Exception err)
{
base.Write(new LogEventInfo
{
Exception = err,
Level = LogLevel.Error,
Message = $"file: {filePath}, dir: {dirPath}, mess: {err.Message}",
LoggerName = "SelfCleaningTarget"
});
}
}
}

View File

@ -0,0 +1,396 @@
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Common.Logging;
public class Log : ILog
{
public string Name { get; set; }
public bool IsDebugEnabled { get; private set; }
public bool IsInfoEnabled { get; private set; }
public bool IsWarnEnabled { get; private set; }
public bool IsErrorEnabled { get; private set; }
public bool IsFatalEnabled { get; private set; }
public bool IsTraceEnabled { get; private set; }
public string LogDirectory => log4net.GlobalContext.Properties["LogDirectory"].ToString();
private log4net.ILog _logger;
public Log(string name)
{
Configure(name);
}
static Log()
{
XmlConfigurator.Configure(log4net.LogManager.GetRepository(Assembly.GetCallingAssembly()));
}
public void Configure(string name)
{
_logger = log4net.LogManager.GetLogger(Assembly.GetCallingAssembly(), name);
IsDebugEnabled = _logger.IsDebugEnabled;
IsInfoEnabled = _logger.IsInfoEnabled;
IsWarnEnabled = _logger.IsWarnEnabled;
IsErrorEnabled = _logger.IsErrorEnabled;
IsFatalEnabled = _logger.IsFatalEnabled;
IsTraceEnabled = _logger.Logger.IsEnabledFor(Level.Trace);
}
public void Trace(object message)
{
if (IsTraceEnabled)
{
_logger.Logger.Log(GetType(), Level.Trace, message, null);
}
}
public void TraceFormat(string message, object arg0)
{
if (IsTraceEnabled)
{
_logger.Logger.Log(GetType(), Level.Trace, string.Format(message, arg0), null);
}
}
public void Debug(object message)
{
if (IsDebugEnabled)
{
_logger.Debug(message);
}
}
public void Debug(object message, Exception exception)
{
if (IsDebugEnabled)
{
_logger.Debug(message, exception);
}
}
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(format, args);
}
}
public void DebugFormat(string format, object arg0)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(format, arg0);
}
}
public void DebugFormat(string format, object arg0, object arg1)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(format, arg0, arg1);
}
}
public void DebugFormat(string format, object arg0, object arg1, object arg2)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(format, arg0, arg1, arg2);
}
}
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsDebugEnabled)
{
_logger.DebugFormat(provider, format, args);
}
}
public void DebugWithProps(string message, IEnumerable<KeyValuePair<string, object>> props)
{
if (!IsDebugEnabled)
{
return;
}
foreach (var p in props)
{
log4net.ThreadContext.Properties[p.Key] = p.Value;
}
_logger.Debug(message);
}
public void Info(object message)
{
if (IsInfoEnabled)
{
_logger.Info(message);
}
}
public void Info(string message, Exception exception)
{
if (IsInfoEnabled)
{
_logger.Info(message, exception);
}
}
public void InfoFormat(string format, params object[] args)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(format, args);
}
}
public void InfoFormat(string format, object arg0)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(format, arg0);
}
}
public void InfoFormat(string format, object arg0, object arg1)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(format, arg0, arg1);
}
}
public void InfoFormat(string format, object arg0, object arg1, object arg2)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(format, arg0, arg1, arg2);
}
}
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsInfoEnabled)
{
_logger.InfoFormat(provider, format, args);
}
}
public void Warn(object message)
{
if (IsWarnEnabled)
{
_logger.Warn(message);
}
}
public void Warn(object message, Exception exception)
{
if (IsWarnEnabled)
{
_logger.Warn(message, exception);
}
}
public void WarnFormat(string format, params object[] args)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(format, args);
}
}
public void WarnFormat(string format, object arg0)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(format, arg0);
}
}
public void WarnFormat(string format, object arg0, object arg1)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(format, arg0, arg1);
}
}
public void WarnFormat(string format, object arg0, object arg1, object arg2)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(format, arg0, arg1, arg2);
}
}
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsWarnEnabled)
{
_logger.WarnFormat(provider, format, args);
}
}
public void Error(object message)
{
if (IsErrorEnabled)
{
_logger.Error(message);
}
}
public void Error(object message, Exception exception)
{
if (IsErrorEnabled)
{
_logger.Error(message, exception);
}
}
public void ErrorFormat(string format, params object[] args)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(format, args);
}
}
public void ErrorFormat(string format, object arg0)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(format, arg0);
}
}
public void ErrorFormat(string format, object arg0, object arg1)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(format, arg0, arg1);
}
}
public void ErrorFormat(string format, object arg0, object arg1, object arg2)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(format, arg0, arg1, arg2);
}
}
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsErrorEnabled)
{
_logger.ErrorFormat(provider, format, args);
}
}
public void Fatal(object message)
{
if (IsFatalEnabled)
{
_logger.Fatal(message);
}
}
public void Fatal(string message, Exception exception)
{
if (IsFatalEnabled)
{
_logger.Fatal(message, exception);
}
}
public void FatalFormat(string format, params object[] args)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(format, args);
}
}
public void FatalFormat(string format, object arg0)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(format, arg0);
}
}
public void FatalFormat(string format, object arg0, object arg1)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(format, arg0, arg1);
}
}
public void FatalFormat(string format, object arg0, object arg1, object arg2)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(format, arg0, arg1, arg2);
}
}
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
if (IsFatalEnabled)
{
_logger.FatalFormat(provider, format, args);
}
}
public void DebugWithProps(string message, KeyValuePair<string, object> prop1, KeyValuePair<string, object> prop2, KeyValuePair<string, object> prop3)
{
if (!IsDebugEnabled)
{
return;
}
AddProp(prop1);
AddProp(prop2);
AddProp(prop3);
_logger.Debug(message);
static void AddProp(KeyValuePair<string, object> p)
{
log4net.ThreadContext.Properties[p.Key] = p.Value;
}
}
}

View File

@ -1,13 +1,5 @@
{
"components": [
{
"type": "ASC.Common.Logging.LogNLog, ASC.Common",
"services": [
{
"type": "ASC.Common.Logging.ILog, ASC.Common"
}
]
},
{
"type": "ASC.Data.Encryption.Crypt, ASC.Data.Encryption",
"services": [