added logging for startup issues
This commit is contained in:
@ -17,7 +17,7 @@ using Oqtane.Repository;
|
||||
using Oqtane.Shared;
|
||||
using Oqtane.Enums;
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// ReSharper disable ConvertToUsingDeclaration
|
||||
@ -33,14 +33,16 @@ namespace Oqtane.Infrastructure
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
private readonly IMemoryCache _cache;
|
||||
private readonly IConfigManager _configManager;
|
||||
private readonly ILogger<DatabaseManager> _filelogger;
|
||||
|
||||
public DatabaseManager(IConfigurationRoot config, IServiceScopeFactory serviceScopeFactory, IWebHostEnvironment environment, IMemoryCache cache, IConfigManager configManager)
|
||||
public DatabaseManager(IConfigurationRoot config, IServiceScopeFactory serviceScopeFactory, IWebHostEnvironment environment, IMemoryCache cache, IConfigManager configManager, ILogger<DatabaseManager> filelogger)
|
||||
{
|
||||
_config = config;
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_environment = environment;
|
||||
_cache = cache;
|
||||
_configManager = configManager;
|
||||
_filelogger = filelogger;
|
||||
}
|
||||
|
||||
public Installation IsInstalled()
|
||||
@ -137,8 +139,8 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
if (!string.IsNullOrEmpty(installation.Message))
|
||||
{
|
||||
Debug.WriteLine($"Oqtane Error: {installation.Message}");
|
||||
// problem with prior installation
|
||||
_filelogger.LogError(Utilities.LogMessage(this, installation.Message));
|
||||
install.ConnectionString = "";
|
||||
}
|
||||
}
|
||||
@ -641,7 +643,7 @@ namespace Oqtane.Infrastructure
|
||||
tenant.Version = Constants.Version;
|
||||
tenants.UpdateTenant(tenant);
|
||||
|
||||
if (site != null) log.Log(site.SiteId, LogLevel.Trace, this, LogFunction.Create, "Site Created {Site}", site);
|
||||
if (site != null) log.Log(site.SiteId, Shared.LogLevel.Information, this, LogFunction.Create, "Site Created {Site}", site);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,18 +46,14 @@ namespace Oqtane.Infrastructure
|
||||
public void Log(int siteId, LogLevel level, object @class, LogFunction function, Exception exception, string message, params object[] args)
|
||||
{
|
||||
Log log = new Log();
|
||||
if (siteId == -1)
|
||||
|
||||
log.SiteId = siteId;
|
||||
if (log.SiteId == -1 && _alias != null)
|
||||
{
|
||||
log.SiteId = null;
|
||||
if (_alias != null)
|
||||
{
|
||||
log.SiteId = _alias.SiteId;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
log.SiteId = siteId;
|
||||
log.SiteId = _alias.SiteId;
|
||||
}
|
||||
if (log.SiteId == -1) return; // logs must be site specific
|
||||
|
||||
log.PageId = null;
|
||||
log.ModuleId = null;
|
||||
log.UserId = null;
|
||||
@ -125,9 +121,10 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
_logs.AddLog(log);
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
// an error occurred writing to the database
|
||||
var x = ex.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
72
Oqtane.Server/Infrastructure/Logging/FileLogger.cs
Normal file
72
Oqtane.Server/Infrastructure/Logging/FileLogger.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
{
|
||||
public class FileLogger : ILogger
|
||||
{
|
||||
protected readonly FileLoggerProvider _FileLoggerProvider;
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
private readonly IConfigManager _configManager;
|
||||
|
||||
public FileLogger(FileLoggerProvider FileLoggerProvider, IWebHostEnvironment environment,IConfigManager configManager)
|
||||
{
|
||||
_FileLoggerProvider = FileLoggerProvider;
|
||||
_environment = environment;
|
||||
_configManager = configManager;
|
||||
}
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel)
|
||||
{
|
||||
return logLevel != LogLevel.None;
|
||||
}
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
if (_configManager.GetSetting("Logging:FileLogger:LogLevel:Default", "") == "")
|
||||
{
|
||||
_configManager.AddOrUpdateSetting("Logging:FileLogger:LogLevel:Default", "Error", true);
|
||||
if (logLevel < LogLevel.Error)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!IsEnabled(logLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string folder = Path.Combine(_environment.ContentRootPath, "Content", "Log");
|
||||
|
||||
// ensure directory exists
|
||||
if (!Directory.Exists(folder))
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
}
|
||||
|
||||
var filepath = Path.Combine(folder, "error.log");
|
||||
|
||||
var logentry = string.Format("{0} [{1}] {2} {3}", "[" + DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss+00:00") + "]", logLevel.ToString(), formatter(state, exception), exception != null ? exception.StackTrace : "");
|
||||
|
||||
try
|
||||
{
|
||||
using (var streamWriter = new StreamWriter(filepath, true))
|
||||
{
|
||||
streamWriter.WriteLine(logentry);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// error occurred
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
30
Oqtane.Server/Infrastructure/Logging/FileLoggerProvider.cs
Normal file
30
Oqtane.Server/Infrastructure/Logging/FileLoggerProvider.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// FileLogger should only be used in scenarios where a database is not available or tenant/site cannot be determined ( ie. during startup )
|
||||
/// </summary>
|
||||
[ProviderAlias("FileLogger")]
|
||||
public class FileLoggerProvider : ILoggerProvider
|
||||
{
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
private readonly IConfigManager _configManager;
|
||||
|
||||
public FileLoggerProvider(IWebHostEnvironment environment, IConfigManager configManager)
|
||||
{
|
||||
_environment = environment;
|
||||
_configManager = configManager;
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
return new FileLogger(this, _environment, _configManager);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -81,7 +81,7 @@ namespace Oqtane.Infrastructure
|
||||
public void SetTenant(int tenantId)
|
||||
{
|
||||
// background processes can set the alias using the SiteState service
|
||||
_siteState.Alias = new Alias { TenantId = tenantId };
|
||||
_siteState.Alias = new Alias { TenantId = tenantId, AliasId = -1, SiteId = -1 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,9 @@ namespace Oqtane.Infrastructure
|
||||
case "2.1.0":
|
||||
Upgrade_2_1_0(tenant, scope);
|
||||
break;
|
||||
case "2.2.0":
|
||||
Upgrade_2_2_0(tenant, scope);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -150,5 +153,15 @@ namespace Oqtane.Infrastructure
|
||||
}
|
||||
}
|
||||
|
||||
private void Upgrade_2_2_0(Tenant tenant, IServiceScope scope)
|
||||
{
|
||||
if (tenant.Name == TenantNames.Master)
|
||||
{
|
||||
if (_configManager.GetSetting("Logging:LogLevel:Default", "") == "")
|
||||
{
|
||||
_configManager.AddOrUpdateSetting("Logging:LogLevel:Default", "Information", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user