Exemplo n.º 1
0
        /// <summary>
        ///     Returns a preconfigured instance of serilog logger.
        ///     Audit log messages will be inserted into a local sqlite database.
        ///     Error messages on the other hand will be added on top of a rolling file log.
        ///     Graylog messages will be sent to a Graylog server (if one exists).
        /// </summary>
        /// <param name="niasMessageAudit"></param>
        /// <returns></returns>
        public ILogger GetLogger(SerilogLogTypesEnum niasMessageAudit)
        {
            var rollingLogLogPath =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogRollingLogLogPath");
            var rollingLogLogFileName =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogRollingLogLogFileName");
            var rollingLogLogTemplate =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogRollingLogLogTemplate");
            var SqliteErrorLogPath =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogSqlLiteErrorLogPath");
            var SqliteErrorLogFileName =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogSqlLiteErrorLogFileName");
            var SqliteAuditLogPath =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogSqlLiteAuditLogPath");
            var SqliteAuditLogFileName =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogSqlLiteAuditLogFileName");
            var SqlitePerformanceLogPath =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogSqlLitePerformanceLogPath");
            var SqlitePerformanceLogFileName =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogSqlLitePerformanceLogFileName");
            var shouldUseGraylog =
                _configurationRepository.GetConfigurationValueOrDefaultAndNotifyIfPropertyNotFound("SerilogUsesGraylog",
                                                                                                   false);
            var graylogIP =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>(
                    "SerilogGraylogAdress");
            var graylogPort =
                _configurationRepository.GetConfigurationValueAndNotifyIfPropertyNotFound <string>("SerilogGraylogPort");

            switch (niasMessageAudit)
            {
            case SerilogLogTypesEnum.PerformanceLog:
                if (!string.IsNullOrEmpty(SqliteAuditLogPath) && !string.IsNullOrEmpty(SqliteAuditLogFileName))
                {
                    _logConfiguration = new LoggerConfiguration().WriteTo.SQLitePerformanceAudit(
                        $"{SqlitePerformanceLogPath}{SqlitePerformanceLogFileName}");

                    if (_memoryCacheProvider.Get <ILogger>("PerformanceLog") == null)
                    {
                        _internalSerilogLogger = _logConfiguration.CreateLogger();
                        _memoryCacheProvider.Save(_internalSerilogLogger, "PerformanceLog");
                    }
                    else
                    {
                        return(_memoryCacheProvider.Get <ILogger>("PerformanceLog"));
                    }
                }

                break;

            case SerilogLogTypesEnum.Graylog:
                if (shouldUseGraylog)
                {
                    if (!string.IsNullOrEmpty(graylogIP) && !string.IsNullOrEmpty(graylogPort))
                    {
                        _logConfiguration = new LoggerConfiguration().WriteTo.Graylog(new GraylogSinkOptions
                        {
                            HostnameOrAdress = graylogIP,
                            Port             = Convert.ToInt32(graylogPort)
                        });

                        if (_memoryCacheProvider.Get <ILogger>("GraylogLog") == null)
                        {
                            _internalSerilogLogger = _logConfiguration.CreateLogger();
                            _memoryCacheProvider.Save(_internalSerilogLogger, "GraylogLog");
                        }
                        else
                        {
                            return(_memoryCacheProvider.Get <ILogger>("GraylogLog"));
                        }
                    }
                    else
                    {
                        _internalSerilogLogger.Error(Err_Graylog_Settings_Invalid);
                    }
                }

                break;

            case SerilogLogTypesEnum.ErrorRollingLog:
                if (!string.IsNullOrEmpty(rollingLogLogPath) && !string.IsNullOrEmpty(rollingLogLogFileName))
                {
                    _logConfiguration = new LoggerConfiguration().WriteTo.RollingFile(
                        rollingLogLogPath + rollingLogLogFileName, outputTemplate: rollingLogLogTemplate);

                    _internalSerilogLogger = _logConfiguration.CreateLogger();

                    if (_memoryCacheProvider.Get <ILogger>("ErrorLogInFile") == null)
                    {
                        _memoryCacheProvider.Save(_internalSerilogLogger, "ErrorLogInFile");
                    }
                    else
                    {
                        return(_memoryCacheProvider.Get <ILogger>("ErrorLogInFile"));
                    }
                }
                break;
            }

            return(_internalSerilogLogger);
        }