Ejemplo n.º 1
0
        private void LogQueueSize(object state)
        {
            try
            {
                QueueLogger queueLogger = (QueueLogger)state;
                string      message     = $"{queueLogger.appender.Name}-Size={queueLogger.Queue.Count}";
                queueLogger.appender.log.Inf(message, queueLogger.appender.LogMessageToFile);

                HttpRequest($"{{\"Msg\":\"{message}\",\"{appender.coreFields.DateFieldName}\":\"{DateTime.UtcNow.ToString("o")}\"}}");
            }
            catch (Exception)
            {
                //continue
            }
        }
Ejemplo n.º 2
0
        public override void ActivateOptions()
        {
            try
            {
                configSettings = new ConfigSettings(this.Name);

                LogMessageToFile   = configSettings.ALALogMessageToFile == null ? LogMessageToFile : (bool)configSettings.ALALogMessageToFile;
                DisableInfoLogFile = configSettings.ALADisableInfoAppenderFile == null ? DisableInfoLogFile : (bool)configSettings.ALADisableInfoAppenderFile;

                string internalLog4NetConfig = "Log4ALA.internalLog4net.config";
                if (DisableInfoLogFile)
                {
                    internalLog4NetConfig = "Log4ALA.internalLog4netOnlyErr.config";
                }

#if !NETSTANDARD2_0 && !NETCOREAPP2_0
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(internalLog4NetConfig))
                {
                    XmlConfigurator.Configure(stream);
                }

                log = LogManager.GetLogger("Log4ALAInternalLogger");
#else
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(internalLog4NetConfig))
                {
                    XmlConfigurator.Configure(LogManager.GetRepository(Assembly.GetEntryAssembly()), stream);
                }

                log = LogManager.GetLogger(REPOSITORY.Name, "Log4ALAInternalLogger");
#endif

                string setErrAppFileNameMessage, setInfoAppFileNameMessage = null;
                bool   isErrFile = SetAppenderFileNameIfAvailable(string.IsNullOrWhiteSpace(configSettings.ALAErrAppenderFile) ? ErrAppenderFile : configSettings.ALAErrAppenderFile, ConfigSettings.LOG_ERR_APPENDER, out setErrAppFileNameMessage);

                bool isInfoFile = false;
                if (!DisableInfoLogFile)
                {
                    isInfoFile = SetAppenderFileNameIfAvailable(string.IsNullOrWhiteSpace(configSettings.ALAInfoAppenderFile) ? InfoAppenderFile : configSettings.ALAInfoAppenderFile, ConfigSettings.LOG_INFO_APPENDER, out setInfoAppFileNameMessage);
                }

                if (isErrFile)
                {
                    log.Inf(setErrAppFileNameMessage, true);
                }
                else
                {
                    System.Console.WriteLine(setErrAppFileNameMessage);
                    log.Err(setErrAppFileNameMessage);
                    extraLog.Err(setErrAppFileNameMessage);
                }

                if (isInfoFile)
                {
                    log.Inf(setInfoAppFileNameMessage, true);
                }
                else
                {
                    if (!DisableInfoLogFile)
                    {
                        System.Console.WriteLine(setInfoAppFileNameMessage);
                        log.Err(setInfoAppFileNameMessage);
                        extraLog.Err(setInfoAppFileNameMessage);
                    }
                }

                if (!string.IsNullOrWhiteSpace(configSettings.ALAErrLoggerName))
                {
#if NETSTANDARD2_0 || NETCOREAPP2_0
                    extraLog = LogManager.GetLogger(REPOSITORY.Name, configSettings.ALAErrLoggerName);
#else
                    extraLog = LogManager.GetLogger(configSettings.ALAErrLoggerName);
#endif
                    log.Inf($"[{this.Name}] - errLoggerName:[{configSettings.ALAErrLoggerName}]", true);
                }
                else if (!string.IsNullOrWhiteSpace(ErrLoggerName))
                {
#if NETSTANDARD2_0 || NETCOREAPP2_0
                    extraLog = LogManager.GetLogger(REPOSITORY.Name, ErrLoggerName);
#else
                    extraLog = LogManager.GetLogger(ErrLoggerName);
#endif
                    log.Inf($"[{this.Name}] - errLoggerName:[{ErrLoggerName}]", true);
                }

#if NETSTANDARD2_0 || NETCOREAPP2_0
                log.Inf($"[{this.Name}] - appsettings directory:[{ConfigSettings.ContentRootPath}]", true);
                log.Inf($"[{this.Name}] - ASPNETCORE_ENVIRONMENT:[{ConfigSettings.AspNetCoreEnvironment}]", true);
                log.Inf($"[{this.Name}] - APPSETTINGS_SUFFIX:[{ConfigSettings.AppsettingsSuffix}]", true);

                if (ConfigSettings.ALAEnableDebugConsoleLog)
                {
                    System.Console.WriteLine($@"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff")}|Log4ALA|TRACE|[{this.Name}] - appsettings directory:[{ConfigSettings.ContentRootPath}]");
                    System.Console.WriteLine($@"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff")}|Log4ALA|TRACE|[{this.Name}] - ASPNETCORE_ENVIRONMENT:[{ConfigSettings.AspNetCoreEnvironment}]");
                    System.Console.WriteLine($@"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff")}|Log4ALA|TRACE|[{this.Name}] - APPSETTINGS_SUFFIX:[{ConfigSettings.AppsettingsSuffix}]");
                }
#endif
                if (string.IsNullOrWhiteSpace(configSettings.ALAWorkspaceId) && string.IsNullOrWhiteSpace(WorkspaceId))
                {
                    throw new Exception($"the Log4ALAAppender property workspaceId [{WorkspaceId}] shouldn't be empty");
                }

                WorkspaceId = string.IsNullOrWhiteSpace(configSettings.ALAWorkspaceId) ? WorkspaceId : configSettings.ALAWorkspaceId;
                log.Inf($"[{this.Name}] - workspaceId:[{WorkspaceId}]", true);


                if (string.IsNullOrWhiteSpace(configSettings.ALASharedKey) && string.IsNullOrWhiteSpace(SharedKey))
                {
                    throw new Exception($"the Log4ALAAppender property sharedKey [{SharedKey}] shouldn't be empty");
                }

                SharedKey = string.IsNullOrWhiteSpace(configSettings.ALASharedKey) ? SharedKey : configSettings.ALASharedKey;
                log.Inf($"[{this.Name}] - sharedKey:[{SharedKey.Remove(15)}...]", true);

                if (string.IsNullOrWhiteSpace(configSettings.ALALogType) && string.IsNullOrWhiteSpace(LogType))
                {
                    throw new Exception($"the Log4ALAAppender property logType [{LogType}] shouldn't be empty");
                }

                LogType = string.IsNullOrWhiteSpace(configSettings.ALALogType) ? LogType : configSettings.ALALogType;
                log.Inf($"[{this.Name}] - logType:[{LogType}]", true);

                AzureApiVersion = string.IsNullOrWhiteSpace(configSettings.ALAAzureApiVersion) ? AzureApiVersion : configSettings.ALAAzureApiVersion;
                log.Inf($"[{this.Name}] - azureApiVersion:[{AzureApiVersion}]", true);

                CoreFieldNames = string.IsNullOrWhiteSpace(configSettings.ALACoreFieldNames) ? CoreFieldNames : configSettings.ALACoreFieldNames;
                log.Inf($"[{this.Name}] - coreFieldNames:[{CoreFieldNames}]", true);

                DebugHTTPReqURI = string.IsNullOrWhiteSpace(configSettings.ALADebugHttpReqUri) ? DebugHTTPReqURI : configSettings.ALADebugHttpReqUri;

                DisableAnonymousPropsPrefix = configSettings.ALADisableAnonymousPropsPrefix == null ? DisableAnonymousPropsPrefix : (bool)configSettings.ALADisableAnonymousPropsPrefix;


                ThreadPriority = string.IsNullOrWhiteSpace(configSettings.ALAThreadPriority) ? ThreadPriority : configSettings.ALAThreadPriority;
                ThreadPriority priority;
                if (!Enum.TryParse(ThreadPriority, out priority))
                {
                    throw new Exception($"the Log4ALAAppender wrong threadPriority value [{ThreadPriority}] possible values -> Lowest/BelowNormal/Normal/AboveNormal/Highest");
                }
                log.Inf($"[{this.Name}] - threadPriority:[{ThreadPriority}]", true);

                HttpDataCollectorRetry = configSettings.ALAHttpDataCollectorRetry == null ? HttpDataCollectorRetry : (int)configSettings.ALAHttpDataCollectorRetry;
                log.Inf($"[{this.Name}] - httpDataCollectorRetry:[{HttpDataCollectorRetry}]", true);

                BatchSizeInBytes = configSettings.ALABatchSizeInBytes == null ? BatchSizeInBytes : (int)configSettings.ALABatchSizeInBytes;
                log.Inf($"[{this.Name}] - batchSizeInBytes:[{BatchSizeInBytes}]", true);

                BatchNumItems = configSettings.ALABatchNumItems == null ? BatchNumItems : (int)configSettings.ALABatchNumItems;

                BatchWaitInSec = configSettings.ALABatchWaitInSec == null ? BatchWaitInSec : (int)configSettings.ALABatchWaitInSec;
                log.Inf($"[{this.Name}] - batchWaitInSec:[{BatchWaitInSec}]", true);

                LoggingQueueSize = configSettings.ALALoggingQueueSize == null ? LoggingQueueSize : (int)configSettings.ALALoggingQueueSize;
                log.Inf($"[{this.Name}] - loggingQueueSize:[{LoggingQueueSize}]", true);

                BatchWaitMaxInSec = configSettings.ALABatchWaitMaxInSec == null ? BatchWaitMaxInSec : (int)configSettings.ALABatchWaitMaxInSec;
                log.Inf($"[{this.Name}] - batchWaitMaxInSec:[{BatchWaitMaxInSec}]", true);

                MaxFieldByteLength = configSettings.ALAMaxFieldByteLength == null ? MaxFieldByteLength : (int)configSettings.ALAMaxFieldByteLength;
                if (MaxFieldByteLength > ConfigSettings.DEFAULT_MAX_FIELD_BYTE_LENGTH)
                {
                    MaxFieldByteLength = ConfigSettings.DEFAULT_MAX_FIELD_BYTE_LENGTH;
                }
                log.Inf($"[{this.Name}] - maxFieldByteLength:[{MaxFieldByteLength}]", true);


                MaxFieldNameLength = configSettings.ALAMaxFieldNameLength == null ? MaxFieldNameLength : (int)configSettings.ALAMaxFieldNameLength;
                if (MaxFieldNameLength > ConfigSettings.DEFAULT_MAX_FIELD_NAME_LENGTH)
                {
                    MaxFieldNameLength = ConfigSettings.DEFAULT_MAX_FIELD_NAME_LENGTH;
                }
                log.Inf($"[{this.Name}] - maxFieldNameLength:[{MaxFieldNameLength}]", true);

                if (BatchSizeInBytes > 0 || BatchWaitInSec > 0)
                {
                    BatchNumItems = 0;
                }

                QueueReadTimeout = configSettings.ALAQueueReadTimeout == null ? QueueReadTimeout : configSettings.ALAQueueReadTimeout;
                log.Inf($"[{this.Name}] - queueReadTimeout:[{QueueReadTimeout}]", true);

                log.Inf($"[{this.Name}] - batchNumItems:[{BatchNumItems}]", true);


                AppendLogger = configSettings.ALAAppendLogger == null ? AppendLogger : (bool)configSettings.ALAAppendLogger;
                log.Inf($"[{this.Name}] - appendLogger:[{AppendLogger}]", true);

                AppendLogLevel = configSettings.ALAAppendLogLevel == null ? AppendLogLevel : (bool)configSettings.ALAAppendLogLevel;
                log.Inf($"[{this.Name}] - appendLogLevel:[{AppendLogLevel}]", true);

                KeyValueDetection = configSettings.ALAKeyValueDetection == null ? KeyValueDetection : (bool)configSettings.ALAKeyValueDetection;
                log.Inf($"[{this.Name}] - keyValueDetection:[{KeyValueDetection}]", true);

                JsonDetection = configSettings.ALAJsonDetection == null ? JsonDetection : (bool)configSettings.ALAJsonDetection;
                log.Inf($"[{this.Name}] - jsonDetecton:[{JsonDetection}]", true);

                log.Inf($"[{this.Name}] - abortTimeoutSeconds:[{ConfigSettings.AbortTimeoutSeconds}]", true);
                log.Inf($"[{this.Name}] - logMessageToFile:[{LogMessageToFile}]", true);

                KeyValueSeparator = string.IsNullOrWhiteSpace(configSettings.ALAKeyValueSeparator) ? KeyValueSeparator : configSettings.ALAKeyValueSeparator;
                log.Inf($"[{this.Name}] - keyValueSeparator:[{KeyValueSeparator}]", true);

                if (KeyValueSeparator.Length > 1 && KeyValueSeparators == null)
                {
                    KeyValueSeparators = new string[] { KeyValueSeparator };
                }

                KeyValuePairSeparator = string.IsNullOrWhiteSpace(configSettings.ALAKeyValuePairSeparator) ? KeyValuePairSeparator : configSettings.ALAKeyValuePairSeparator;
                log.Inf($"[{this.Name}] - keyValuePairSeparator:[{KeyValuePairSeparator}]", true);

                if (KeyValuePairSeparator.Length > 1 && KeyValuePairSeparators == null)
                {
                    KeyValuePairSeparators = new string[] { KeyValuePairSeparator };
                }


                string configDisableInfoLogFile = configSettings.ALADisableInfoAppenderFileCommon ? "CommonConfiguration" : this.Name;
                log.Inf($"[{configDisableInfoLogFile}] - disableInfoLogFile:[{DisableInfoLogFile}]", true);

                string configDisableAnonymousPropsPrefix = configSettings.ALADisableAnonymousPropsPrefixCommon ? "CommonConfiguration" : this.Name;
                log.Inf($"[{configDisableAnonymousPropsPrefix}] - disableAnonymousPropsPrefix:[{DisableAnonymousPropsPrefix}]", true);

                if (!string.IsNullOrWhiteSpace(DebugHTTPReqURI))
                {
                    string configDebugHTTPReqURI = configSettings.ALADebugHttpReqUriCommon ? "CommonConfiguration" : this.Name;
                    log.Inf($"[{configDebugHTTPReqURI}] - debugHTTPReqURI:[{DebugHTTPReqURI}]", true);
                }

                log.Inf($"[CommonConfiguration] - alaQueueSizeLogIntervalEnabled:[{ConfigSettings.IsLogQueueSizeInterval}]", true);
                log.Inf($"[CommonConfiguration] - alaQueueSizeLogIntervalInSec:[{ConfigSettings.LogQueueSizeInterval}]", true);
                log.Inf($"[CommonConfiguration] - enableDebugConsoleLog:[{ConfigSettings.ALAEnableDebugConsoleLog}]", true);



                serializer = new LoggingEventSerializer(this);

                queueLogger = new QueueLogger(this);
            }
            catch (Exception ex)
            {
                queueLogger = null;
                string message = $"[{this.Name}] - Unable to activate Log4ALAAppender: [{ex.Message}]";
                System.Console.WriteLine(message);
                log.Err(message);
                extraLog.Err(message);
            }
        }