public static void Main(string[] args)
        {
            try
            {
                ThreadPool.SetMinThreads(10000, 500);
                ServicePointManager.DefaultConnectionLimit = int.MaxValue;
                ServicePointManager.UseNagleAlgorithm      = false;
                ServicePointManager.Expect100Continue      = false;

                LoggingSource.Initialize(ServiceEventSource.Current.Message);

                ServiceRuntime.RegisterServiceAsync(
                    "StockServiceType",
                    context =>
                    new StockService(context)).GetAwaiter().GetResult();

                ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(StockService).Name);

                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                throw;
            }
        }
예제 #2
0
        /// <summary>
        /// 这是服务主机进程的入口点。
        /// </summary>
        private static void Main()
        {
            try
            {
                // ServiceManifest.XML 文件定义一个或多个服务类型名称。
                // 注册服务会将服务类型名称映射到 .NET 类型。
                // 在 Service Fabric 创建此服务类型的实例时,
                // 会在此主机进程中创建类的实例。

                ThreadPool.SetMinThreads(10000, 500);//线程池 最小

                ServicePointManager.DefaultConnectionLimit = int.MaxValue;
                ServicePointManager.UseNagleAlgorithm      = false;
                ServicePointManager.Expect100Continue      = false;

                LoggingSource.Initialize(ServiceEventSource.Current.Message);

                ServiceRuntime.RegisterServiceAsync("PublicGateType",
                                                    context => new PublicGate(context)).GetAwaiter().GetResult();

                ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(PublicGate).Name);

                // 防止此主机进程终止,以使服务保持运行。
                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                throw;
            }
        }
예제 #3
0
        public async Task LoggingSource_WhileLogging_ShouldNotLoseLogFile(bool compressing)
        {
            var name = GetTestName();

            var path          = NewDataPath(forceCreateDir: true);
            var retentionTime = TimeSpan.MaxValue;
            var retentionSize = long.MaxValue;
            var loggingSource = new LoggingSource(
                LogMode.Information,
                path,
                "LoggingSource" + name,
                retentionTime,
                retentionSize,
                compressing);

            loggingSource.MaxFileSizeInBytes = 1024;
            //This is just to make sure the MaxFileSizeInBytes is get action for the first file
            loggingSource.SetupLogMode(LogMode.Operations, path, retentionTime, retentionSize, compressing);

            var logger = new Logger(loggingSource, "Source" + name, "Logger" + name);

            for (var i = 0; i < 1000; i++)
            {
                await logger.OperationsAsync("Some message");
            }

            var beforeEndFiles = Directory.GetFiles(path);

            loggingSource.EndLogging();

            var afterEndFiles = Directory.GetFiles(path);

            AssertNoFileMissing(beforeEndFiles);
            AssertNoFileMissing(afterEndFiles);
        }
예제 #4
0
 public static void WriteLogEvent(LoggingSource Source, string ClassMethod, string Message,
                                  LoggingOperationType OperationType, LoggingLevel Level,
                                  Nullable <Guid> IdArchive, Nullable <Guid> IdStorage, Nullable <Guid> IdCorrelation)
 {
     WriteLogEvent(Source, ClassMethod, Message,
                   OperationType, Level,
                   IdArchive, IdStorage, IdCorrelation, Dns.GetHostName(), string.Empty);
 }
예제 #5
0
        public static void WriteLogEvent(LoggingSource Source, string ClassMethod, string Message,
                                         LoggingOperationType OperationType, LoggingLevel Level,
                                         Nullable <Guid> IdArchive, Nullable <Guid> IdStorage, Nullable <Guid> IdCorrelation, string Server, String Client)
        {
            try
            {
                switch (Level)
                {
                case LoggingLevel.BiblosDS_Information:
                case LoggingLevel.BiblosDS_Trace:
#if DEBUG
                    EventLogHelper.GetUniqueInstance.Write("Source : " + Source.ToString() +
                                                           " ClassMethod : " + ClassMethod +
                                                           "\nMessage : " + Message, System.Diagnostics.EventLogEntryType.Information);
#endif
                    break;

                default:
                case LoggingLevel.BiblosDS_Warning:
                case LoggingLevel.BiblosDS_Managed_Error:
                    EventLogHelper.GetUniqueInstance.Write("Source : " + Source.ToString() +
                                                           " ClassMethod : " + ClassMethod +
                                                           "\nMessage : " + Message, System.Diagnostics.EventLogEntryType.Warning);
                    break;

                case LoggingLevel.BiblosDS_Errors:
                    EventLogHelper.GetUniqueInstance.Write("Source : " + Source.ToString() +
                                                           " ClassMethod : " + ClassMethod +
                                                           "\nMessage : " + Message, System.Diagnostics.EventLogEntryType.Error);
                    break;
                }
            }
            catch (Exception)
            {
                //Ignore
            }

            try
            {
                if (ClassMethod.Contains("ConnectionString") == false)
                {
                    // scrive nel database
                    DbProvider.AddLog(OperationType,
                                      Guid.NewGuid(),
                                      IdArchive == null ? Guid.Empty : (Guid)IdArchive,
                                      IdStorage == null ? Guid.Empty : (Guid)IdStorage,
                                      IdCorrelation == null ? Guid.Empty : (Guid)IdCorrelation,
                                      Message, Server, Client);
                }
            }
            catch (Exception)
            {
                //Ignore
            }
        }
예제 #6
0
 public static void WriteLogEvent(LoggingSource Source, string ClassMethod, string Message, LoggingOperationType OperationType, LoggingLevel Level)
 {
     try
     {
         WriteLogEvent(Source, ClassMethod, Message, OperationType, Level, null, null, null);
     }
     catch
     {
         //PUT into Try to avoid exceptions on the call
     }
 }
예제 #7
0
 public static void WriteJournaling(LoggingSource Source, string ClassMethod, string Message,
                                    LoggingOperationType OperationType, LoggingLevel Level, Nullable <Guid> IdCorrelation, Document document)
 {
     if (document == null)
     {
         document = new Document();
     }
     WriteJournaling(Source, "", ClassMethod, Message, LoggingOperationType.BiblosDS_General, Level,
                     document.Archive == null ? Guid.Empty : document.Archive.IdArchive,
                     document.Storage == null ? Guid.Empty : document.Storage.IdStorage, IdCorrelation);
 }
예제 #8
0
        public async Task LoggingSource_WhenExistFileFromToday_ShouldIncrementNumberByOne(string extension)
        {
            const int fileSize = Constants.Size.Kilobyte;

            var testName = GetTestName();
            var path     = NewDataPath(forceCreateDir: true);

            path = Path.Combine(path, Guid.NewGuid().ToString("N"));
            Directory.CreateDirectory(path);

            const long retentionSize = long.MaxValue;
            var        retentionTimeConfiguration = TimeSpan.FromDays(3);

            var now      = DateTime.Now;
            var existLog = Path.Combine(path, $"{LoggingSource.LogInfo.DateToLogFormat(now)}.010.{extension}");

            await using (var file = File.Create(existLog))
            {
                file.SetLength(fileSize);
            }
            var loggingSource = new LoggingSource(
                LogMode.Information,
                path,
                "LoggingSource" + testName,
                retentionTimeConfiguration,
                retentionSize)
            {
                MaxFileSizeInBytes = fileSize
            };

            //This is just to make sure the MaxFileSizeInBytes is get action for the first file
            loggingSource.SetupLogMode(LogMode.Operations, path, retentionTimeConfiguration, retentionSize, false);

            var logger = new Logger(loggingSource, "Source" + testName, "Logger" + testName);
            await logger.OperationsAsync("Some message");

            var result = WaitForValue(() =>
            {
                var strings = Directory.GetFiles(path);
                return(strings.Any(f =>
                {
                    if (LoggingSource.LogInfo.TryGetDate(f, out var d) == false || d.Date.Equals(DateTime.Today) == false)
                    {
                        return false;
                    }

                    return LoggingSource.LogInfo.TryGetNumber(f, out var n) && n == 11;
                }));
            }, true, 10_000, 1_000);

            Assert.True(result);

            loggingSource.EndLogging();
        }
예제 #9
0
 public static void WriteError(LoggingSource Source, string ClassMethod, Exception ex, LoggingOperationType OperationType, Document document)
 {
     try
     {
         WriteLogEvent(Source, ClassMethod, ex.ToString(), OperationType, LoggingLevel.BiblosDS_Errors, document.Archive == null ? Guid.Empty : document.Archive.IdArchive, document.Storage == null ? Guid.Empty : document.Storage.IdStorage, null);
     }
     catch
     {
         //PUT into Try to avoid exceptions on the call
     }
 }
예제 #10
0
        public async Task LoggingSource_WhileRetentionBySizeOn_ShouldKeepRetentionPolicy(bool compressing)
        {
            const int fileSize      = Constants.Size.Kilobyte;
            const int retentionSize = 5 * Constants.Size.Kilobyte;

            var name = GetTestName();

            var path          = NewDataPath(forceCreateDir: true);
            var retentionTime = TimeSpan.MaxValue;
            var loggingSource = new LoggingSource(
                LogMode.Information,
                path,
                "LoggingSource" + name,
                retentionTime,
                retentionSize,
                compressing);

            loggingSource.MaxFileSizeInBytes = fileSize;
            //This is just to make sure the MaxFileSizeInBytes is get action for the first file
            loggingSource.SetupLogMode(LogMode.Operations, path, retentionTime, retentionSize, compressing);

            var logger = new Logger(loggingSource, "Source" + name, "Logger" + name);

            for (var i = 0; i < 100; i++)
            {
                for (var j = 0; j < 5; j++)
                {
                    await logger.OperationsAsync("Some message");
                }
                Thread.Sleep(10);
            }

            const int threshold = 2 * fileSize;
            long      size      = 0;

            string[] afterEndFiles            = null;
            var      isRetentionPolicyApplied = WaitForValue(() =>
            {
                Task.Delay(TimeSpan.FromSeconds(1));

                afterEndFiles = Directory.GetFiles(path);
                AssertNoFileMissing(afterEndFiles);
                size = afterEndFiles.Select(f => new FileInfo(f)).Sum(f => f.Length);

                return(Math.Abs(size - retentionSize) <= threshold);
            }, true, 10_000, 1_000);

            Assert.True(isRetentionPolicyApplied,
                        $"ActualSize({size}), retentionSize({retentionSize}), threshold({threshold})" +
                        Environment.NewLine +
                        JustFileNamesAsString(afterEndFiles));

            loggingSource.EndLogging();
        }
예제 #11
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            LoggingSource loggingSource = LoggingSource.Instance;

            loggingSource.EnableConsoleLogging();


            Logger logger = loggingSource.GetLogger <App>("APP");

            logger.Info("APPLICATION STARTED");
        }
예제 #12
0
        public static void Log(LoggingSource source, string text)
        {
            string prefixedText = string.Format("{0} [{1}] - {2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), source, text);

            if (sourcesToWriteToDebug.Contains(source))
            {
                Debug.WriteLine(prefixedText);

            }

            if (sourcesToWriteToFile.Contains(source))
            {
                VerifyAppFolderExists();
                File.AppendAllText(string.Format(Constants.ApplicationGenericFilePath, "errorlog.txt"), prefixedText);
            }
        }
예제 #13
0
        public async Task Register_WhenLogModeIsNone_ShouldNotWriteToLogFile()
        {
            var name = GetTestName();
            var path = NewDataPath(forceCreateDir: true);

            path = Path.Combine(path, Guid.NewGuid().ToString());
            Directory.CreateDirectory(path);

            var loggingSource = new LoggingSource(
                LogMode.None,
                path,
                "LoggingSource" + name,
                TimeSpan.MaxValue,
                long.MaxValue,
                false);

            var logger = new Logger(loggingSource, "Source" + name, "Logger" + name);
            var tcs    = new TaskCompletionSource <WebSocketReceiveResult>(TaskCreationOptions.RunContinuationsAsynchronously);
            var socket = new DummyWebSocket();

            socket.ReceiveAsyncFunc = () => tcs.Task;
            var context = new LoggingSource.WebSocketContext();

            var _ = loggingSource.Register(socket, context, CancellationToken.None);

            var uniqForOperation   = Guid.NewGuid().ToString();
            var uniqForInformation = Guid.NewGuid().ToString();

            var logTasks = Task.WhenAll(logger.OperationsAsync(uniqForOperation), logger.InfoAsync(uniqForInformation));
            var timeout  = TimeSpan.FromSeconds(10);
            await Task.WhenAny(logTasks, Task.Delay(timeout));

            Assert.True(logTasks.IsCompleted, $"Waited over {timeout.TotalSeconds} seconds for log tasks to finish");

            tcs.SetResult(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true, WebSocketCloseStatus.NormalClosure, ""));
            loggingSource.EndLogging();

            Assert.Contains(uniqForOperation, socket.LogsReceived);
            Assert.Contains(uniqForInformation, socket.LogsReceived);

            var logFile    = Directory.GetFiles(path).First();
            var logContent = await File.ReadAllTextAsync(logFile);

            Assert.DoesNotContain(uniqForOperation, logContent);
            Assert.DoesNotContain(uniqForInformation, logContent);
        }
예제 #14
0
        public async Task LoggingSource_WhenExistFileFromYesterdayAndCreateNewFileForToday_ShouldResetNumberToZero(string extension)
        {
            var testName = GetTestName();
            var path     = NewDataPath(forceCreateDir: true);

            path = Path.Combine(path, Guid.NewGuid().ToString("N"));
            Directory.CreateDirectory(path);

            const long retentionSize = long.MaxValue;
            var        retentionTimeConfiguration = TimeSpan.FromDays(3);

            var yesterday    = DateTime.Now - TimeSpan.FromDays(1);
            var yesterdayLog = Path.Combine(path, $"{LoggingSource.LogInfo.DateToLogFormat(yesterday)}.010.{extension}");
            await File.Create(yesterdayLog).DisposeAsync();

            File.SetLastWriteTime(yesterdayLog, yesterday);

            var loggingSource = new LoggingSource(
                LogMode.Information,
                path,
                "LoggingSource" + testName,
                retentionTimeConfiguration,
                retentionSize);

            var logger = new Logger(loggingSource, "Source" + testName, "Logger" + testName);
            await logger.OperationsAsync("Some message");

            var todayLog = string.Empty;

            WaitForValue(() =>
            {
                var afterEndFiles = Directory.GetFiles(path);
                todayLog          = afterEndFiles.FirstOrDefault(f =>
                                                                 LoggingSource.LogInfo.TryGetDate(f, out var date) && date.Date.Equals(DateTime.Today));
                return(todayLog != null);
            }, true, 10_000, 1_000);

            Assert.True(LoggingSource.LogInfo.TryGetNumber(todayLog, out var n) && n == 0);

            loggingSource.EndLogging();
        }
예제 #15
0
        public ActionResult MinimumLevel(LoggingSource loggingSource, LogEventLevel newMiniumLevel)
        {
            this.logger.LogTrace("LogController: MinimumLevel PUT called");

            switch (loggingSource)
            {
            case LoggingSource.General:
                this.appLoggingLevelSwitch.GerneralLoggingLevelSwitch.MinimumLevel = newMiniumLevel;
                break;

            case LoggingSource.Microsoft:
                this.appLoggingLevelSwitch.MicrosoftLoggingLevelSwitch.MinimumLevel = newMiniumLevel;
                break;

            case LoggingSource.Client:
                this.appLoggingLevelSwitch.ClientLoggingLevelSwitch.MinimumLevel = newMiniumLevel;
                break;
            }

            return(base.NoContent());
        }
예제 #16
0
        public static void Main(string[] args)
        {
            try
            {
                // Initializing ILogger   111
                LoggingSource.Initialize(ServiceEventSource.Current.Message);

                ServiceRuntime.RegisterServiceAsync(
                    "StockAggregatorServiceType",
                    context =>
                    new StockAggregatorService(context)).GetAwaiter().GetResult();

                ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(StockAggregatorService).Name);

                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                throw;
            }
        }
예제 #17
0
        public static void RemoteMessage(LoggingSource loggingSource,
                                         LoggingType loggingType,
                                         string message,
                                         params object[] args)
        {
            string formattedMessage = string.Format(message, args);
            bool   isSuccess;

            if ((loggingSource & LoggingSource.Mail) == LoggingSource.Mail)
            {
                string subject      = loggingType + " логирование с сайта";
                var    mailerConfig = new MailerConfig {
                    IsHtmlBody   = false,
                    DisplayName  = subject,
                    SendToAdmins = true
                };

                var    mailer      = new Mailer.Mailer();
                string mailAddress = loggingType.HasFlag(LoggingType.Error) ? MailAddresses.EXCEPTIONS : MailAddresses.SUPPORT;
                isSuccess = mailer.SendMail(mailAddress, mailAddress, subject, formattedMessage, mailerConfig);

                if (!isSuccess)
                {
                    LogTo(LoggerName.Errors).ErrorFormat(
                        "LoggerWrapper.RemoteMessage не удалось отправить сообщение по почте: {0}",
                        formattedMessage);
                }
            }

            /*if ((loggingSource & LoggingSource.Db) == LoggingSource.Db) {
             *  isSuccess = _loggerQuery.Create(loggingType, formattedMessage);
             *
             *  if (!isSuccess) {
             *      LogTo(LoggerName.Errors).ErrorFormat(
             *          "LoggerWrapper.RemoteMessage не удалось записать сообщение в БД: {0}",
             *          formattedMessage);
             *  }
             * }*/
        }
예제 #18
0
 public static void WriteJournaling(LoggingSource Source, string UserAgent, string ClassMethod, string Message,
                                    LoggingOperationType OperationType, LoggingLevel Level,
                                    Nullable <Guid> IdArchive, Nullable <Guid> IdStorage, Nullable <Guid> IdCorrelation)
 {
     try
     {
         if (ConfigurationManager.AppSettings["JournalEnabled"] == null || ConfigurationManager.AppSettings["JournalEnabled"].ToString() == "true")
         {
             DbProvider.AddJournal(OperationType,
                                   UserAgent,
                                   Guid.NewGuid(),
                                   IdArchive == null ? Guid.Empty : (Guid)IdArchive,
                                   IdStorage == null ? Guid.Empty : (Guid)IdStorage,
                                   IdCorrelation == null ? Guid.Empty : (Guid)IdCorrelation,
                                   Message);
         }
     }
     catch (Exception ex)
     {
         logger.Error(ex);
     }
 }
        public static void Main(string[] args)
        {
            try
            {
                ThreadPool.SetMinThreads(10000, 500);
                ServicePointManager.DefaultConnectionLimit = int.MaxValue;
                ServicePointManager.UseNagleAlgorithm      = false;
                ServicePointManager.Expect100Continue      = false;

                LoggingSource.Initialize(ActorEventSource.Current.Message);


                ActorRuntime.RegisterActorAsync <StockTrendPredictionActor>().GetAwaiter().GetResult();
                ActorRuntime.RegisterActorAsync <NotificationActor>().GetAwaiter().GetResult();

                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception e)
            {
                ActorEventSource.Current.ActorHostInitializationFailed(e.ToString());
                throw;
            }
        }
예제 #20
0
        /// <summary>
        /// Logs information to the console and optionally a log file
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <param name="msg"></param>        
        public virtual string Log(LoggingSource source, LogType type, string msg)
        {
            if (!_config.DebugMode || _paused)
                return String.Empty;

            string output = String.Empty;

            switch (_debugLevel)
            {
                case IMAPConfig.DebugDetailLevel.All:
                    {
                        output = GenerateMessage(type, msg);
                        break;
                    }
                case IMAPConfig.DebugDetailLevel.ErrorsOnly:
                    {
                        if (type == LogType.Error)
                            output = GenerateMessage(type, msg);
                        break;
                    }
                case IMAPConfig.DebugDetailLevel.InterIMAPOnly:
                    {
                        if (source == LoggingSource.InterIMAP || source == LoggingSource.MessageWorker)
                            output = GenerateMessage(type, msg);
                        break;
                    }
            }

            if (output != String.Empty)
            {
                if (_workerID > -1)
                    output = String.Format("Worker {0} - {1}", _workerID, output);
                Console.WriteLine(output);

                if (_config.LogFile != String.Empty)
                {
                    string logFile = _config.LogFile;

                    if (_workerID > -1)
                    {
                        string file = Path.GetFileNameWithoutExtension(_config.LogFile);
                        string path = _config.LogFile.Replace(file, "{0}");
                        file += String.Format("_{0}", _workerID);
                        logFile = String.Format(path, file);
                    }

                    StreamWriter strWriter = new StreamWriter(logFile, true);
                    strWriter.WriteLine("{0} - {1}", DateTime.Now, msg);
                    strWriter.Flush();
                    strWriter.Close();
                }

                if (EventLogged != null)
                {
                    EventLogged(output);
                }
            }

            return output;
        }
예제 #21
0
        public async Task LoggingSource_WhileRetentionByTimeInHours_ShouldKeepRetentionPolicy(bool compressing)
        {
            const int fileSize = Constants.Size.Kilobyte;

            var name = GetTestName();
            var path = NewDataPath(forceCreateDir: true);

            path = Path.Combine(path, Guid.NewGuid().ToString());
            Directory.CreateDirectory(path);
            var retentionTimeConfiguration = TimeSpan.FromHours(3);

            var now             = DateTime.Now;
            var retentionTime   = now - retentionTimeConfiguration;
            var toCheckLogFiles = new List <(string fileName, bool shouldExist)>();

            const int artificialLogsCount = 9;

            for (int i = 0; i < artificialLogsCount; i++)
            {
                var lastModified = now - TimeSpan.FromHours(i);
                var fileName     = Path.Combine(path, $"{LoggingSource.LogInfo.DateToLogFormat(lastModified)}.00{artificialLogsCount - i}.log");
                toCheckLogFiles.Add((fileName, lastModified > retentionTime));
                await using (File.Create(fileName))
                { }
                File.SetLastWriteTime(fileName, lastModified);
            }

            const long retentionSize = long.MaxValue;
            var        loggingSource = new LoggingSource(
                LogMode.Information,
                path,
                "LoggingSource" + name,
                retentionTimeConfiguration,
                retentionSize,
                compressing)
            {
                MaxFileSizeInBytes = fileSize
            };

            //This is just to make sure the MaxFileSizeInBytes is get action for the first file
            loggingSource.SetupLogMode(LogMode.Operations, path, retentionTimeConfiguration, retentionSize, compressing);

            var logger = new Logger(loggingSource, "Source" + name, "Logger" + name);

            for (var j = 0; j < 50; j++)
            {
                for (var i = 0; i < 5; i++)
                {
                    await logger.OperationsWithWait("Some message");
                }
                Thread.Sleep(10);
            }

            string[] afterEndFiles = null;
            await WaitForValueAsync(async() =>
            {
                for (var i = 0; i < 10; i++)
                {
                    await logger.OperationsWithWait("Some message");
                }
                afterEndFiles = Directory.GetFiles(path);
                return(DoesContainFilesThatShouldNotBeFound(afterEndFiles, toCheckLogFiles, compressing));
            }, false, 10_000, 1_000);

            loggingSource.EndLogging();

            AssertNoFileMissing(afterEndFiles);

            try
            {
                Assert.All(toCheckLogFiles, toCheck =>
                {
                    (string fileName, bool shouldExist) = toCheck;
                    fileName     = $"{fileName}{(compressing ? ".gz" : string.Empty)}";
                    var fileInfo = new FileInfo(fileName);
                    if (shouldExist)
                    {
                        Assert.True(fileInfo.Exists, $"The log file \"{fileInfo.Name}\" should be exist");
                    }
                    else
                    {
                        Assert.False(fileInfo.Exists, $"The log file \"{fileInfo.Name}\" last modified {fileInfo.LastWriteTime} should not be exist. retentionTime({retentionTime})");
                    }
                });
            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"Logs after end - {JustFileNamesAsString(afterEndFiles)}", e);
            }
        }
예제 #22
0
        /// <summary>
        /// Logs information to the console and optionally a log file
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <param name="msg"></param>
        public virtual string Log(LoggingSource source, LogType type, string msg)
        {
            if (!_config.DebugMode || _paused)
            {
                return(String.Empty);
            }

            string output = String.Empty;

            switch (_debugLevel)
            {
            case IMAPConfig.DebugDetailLevel.All:
            {
                output = GenerateMessage(type, msg);
                break;
            }

            case IMAPConfig.DebugDetailLevel.ErrorsOnly:
            {
                if (type == LogType.Error)
                {
                    output = GenerateMessage(type, msg);
                }
                break;
            }

            case IMAPConfig.DebugDetailLevel.InterIMAPOnly:
            {
                if (source == LoggingSource.InterIMAP || source == LoggingSource.MessageWorker)
                {
                    output = GenerateMessage(type, msg);
                }
                break;
            }
            }

            if (output != String.Empty)
            {
                if (_workerID > -1)
                {
                    output = String.Format("Worker {0} - {1}", _workerID, output);
                }
                Console.WriteLine(output);

                if (_config.LogFile != String.Empty)
                {
                    string logFile = _config.LogFile;

                    if (_workerID > -1)
                    {
                        string file = Path.GetFileNameWithoutExtension(_config.LogFile);
                        string path = _config.LogFile.Replace(file, "{0}");
                        file   += String.Format("_{0}", _workerID);
                        logFile = String.Format(path, file);
                    }

                    StreamWriter strWriter = new StreamWriter(logFile, true);
                    strWriter.WriteLine("{0} - {1}", DateTime.Now, msg);
                    strWriter.Flush();
                    strWriter.Close();
                }

                if (EventLogged != null)
                {
                    EventLogged(output);
                }
            }

            return(output);
        }
예제 #23
0
        public async Task LoggingSource_WhileRetentionBySizeOn_ShouldKeepRetentionPolicy(bool compressing)
        {
            const int fileSize      = Constants.Size.Kilobyte;
            const int retentionSize = 5 * Constants.Size.Kilobyte;

            var name = GetTestName();

            var path          = NewDataPath(forceCreateDir: true);
            var retentionTime = TimeSpan.MaxValue;
            var loggingSource = new LoggingSource(
                LogMode.Information,
                path,
                "LoggingSource" + name,
                retentionTime,
                retentionSize,
                compressing);

            loggingSource.MaxFileSizeInBytes = fileSize;
            //This is just to make sure the MaxFileSizeInBytes is get action for the first file
            loggingSource.SetupLogMode(LogMode.Operations, path, retentionTime, retentionSize, compressing);

            var logger = new Logger(loggingSource, "Source" + name, "Logger" + name);

            for (var i = 0; i < 100; i++)
            {
                for (var j = 0; j < 5; j++)
                {
                    await logger.OperationsAsync("Some message");
                }
                Thread.Sleep(10);
            }

            var sw = Stopwatch.StartNew();

            while (true)
            {
                await Task.Delay(TimeSpan.FromSeconds(1));

                var afterEndFiles = Directory.GetFiles(path);
                AssertNoFileMissing(afterEndFiles);
                var       size      = afterEndFiles.Select(f => new FileInfo(f)).Sum(f => f.Length);
                const int threshold = 2 * fileSize;

                if (Math.Abs(size - retentionSize) <= threshold)
                {
                    break;
                }

                if (sw.Elapsed < TimeSpan.FromSeconds(10))
                {
                    continue;
                }

                Assert.True(false,
                            $"ActualSize({size}), retentionSize({retentionSize}), threshold({threshold})" +
                            Environment.NewLine +
                            JustFileNamesAsString(afterEndFiles));
            }

            loggingSource.EndLogging();
        }
예제 #24
0
        public async Task AttachPipeSink_WhenLogModeIsOperations_ShouldWriteToLogFileJustOperations(LogMode logMode)
        {
            var timeout = TimeSpan.FromSeconds(10);

            var name = GetTestName();
            var path = NewDataPath(forceCreateDir: true);

            path = Path.Combine(path, Guid.NewGuid().ToString());
            Directory.CreateDirectory(path);

            var loggingSource = new LoggingSource(
                logMode,
                path,
                "LoggingSource" + name,
                TimeSpan.MaxValue,
                long.MaxValue,
                false);

            var logger = new Logger(loggingSource, "Source" + name, "Logger" + name);
            var tcs    = new TaskCompletionSource <WebSocketReceiveResult>(TaskCreationOptions.RunContinuationsAsynchronously);

            await using var stream = new MemoryStream();

            //Attach Pipe
            loggingSource.AttachPipeSink(stream);
            var beforeDetachOperation   = Guid.NewGuid().ToString();
            var beforeDetachInformation = Guid.NewGuid().ToString();

            var logTasks = Task.WhenAll(logger.OperationsAsync(beforeDetachOperation), logger.InfoAsync(beforeDetachInformation));
            await Task.WhenAny(logTasks, Task.Delay(timeout));

            Assert.True(logTasks.IsCompleted, $"Waited over {timeout.TotalSeconds} seconds for log tasks to finish");

            //Detach Pipe
            loggingSource.DetachPipeSink();
            await stream.FlushAsync();

            var afterDetachOperation   = Guid.NewGuid().ToString();
            var afterDetachInformation = Guid.NewGuid().ToString();

            logTasks = Task.WhenAll(logger.OperationsAsync(afterDetachOperation), logger.InfoAsync(afterDetachInformation));
            await Task.WhenAny(logTasks, Task.Delay(timeout));

            Assert.True(logTasks.IsCompleted || logMode == LogMode.None,
                        $"Waited over {timeout.TotalSeconds} seconds for log tasks to finish");

            tcs.SetResult(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true, WebSocketCloseStatus.NormalClosure, ""));
            loggingSource.EndLogging();

            var logsFromPipe = Encodings.Utf8.GetString(stream.ToArray());

            Assert.Contains(beforeDetachInformation, logsFromPipe);
            Assert.Contains(beforeDetachOperation, logsFromPipe);
            Assert.DoesNotContain(afterDetachInformation, logsFromPipe);
            Assert.DoesNotContain(afterDetachOperation, logsFromPipe);

            string logsFileContentAfter = await ReadLogsFileContent(path);

            AssertContainsLog(LogMode.Information, logMode)(beforeDetachInformation, logsFileContentAfter);
            AssertContainsLog(LogMode.Operations, logMode)(beforeDetachOperation, logsFileContentAfter);

            AssertContainsLog(LogMode.Information, logMode)(afterDetachInformation, logsFileContentAfter);
            AssertContainsLog(LogMode.Operations, logMode)(afterDetachOperation, logsFileContentAfter);
        }
예제 #25
0
 public static void Log(LoggingSource source, string text, params string[] replacement)
 {
     string replacementExpanded = string.Format(text, replacement);
     Log(source, replacementExpanded);
 }
예제 #26
0
        public async Task LoggingSource_WhileStopAndStartAgain_ShouldNotOverrideOld(bool compressing)
        {
            const int taskTimeout = 10000;
            var       name        = GetTestName();

            var path          = NewDataPath(forceCreateDir: true);
            var retentionTime = TimeSpan.MaxValue;
            var retentionSize = long.MaxValue;

            var firstLoggingSource = new LoggingSource(
                LogMode.Information,
                path,
                "FirstLoggingSource" + name,
                retentionTime,
                retentionSize,
                compressing);

            firstLoggingSource.MaxFileSizeInBytes = 1024;
            //This is just to make sure the MaxFileSizeInBytes is get action for the first file
            firstLoggingSource.SetupLogMode(LogMode.Operations, path, retentionTime, retentionSize, compressing);

            try
            {
                var logger = new Logger(firstLoggingSource, "Source" + name, "Logger" + name);

                for (var i = 0; i < 100; i++)
                {
                    var task = logger.OperationsAsync("Some message");
                    await Task.WhenAny(task, Task.Delay(taskTimeout));

                    if (task.IsCompleted == false)
                    {
                        throw new TimeoutException($"The log task took more then one second");
                    }
                }
            }
            finally
            {
                firstLoggingSource.EndLogging();
            }

            var beforeRestartFiles = Directory.GetFiles(path);

            var restartDateTime = DateTime.Now;


            Exception anotherThreadException = null;
            //To start new LoggingSource the object need to be construct on another thread
            var anotherThread = new Thread(() =>
            {
                var secondLoggingSource = new LoggingSource(
                    LogMode.Information,
                    path,
                    "SecondLoggingSource" + name,
                    retentionTime,
                    retentionSize);

                try
                {
                    secondLoggingSource.MaxFileSizeInBytes = 1024;
                    var secondLogger = new Logger(secondLoggingSource, "Source" + name, "Logger" + name);

                    for (var i = 0; i < 100; i++)
                    {
                        var task = secondLogger.OperationsAsync("Some message");
                        Task.WhenAny(task, Task.Delay(taskTimeout)).GetAwaiter().GetResult();
                        if (task.IsCompleted == false)
                        {
                            throw new TimeoutException($"The log task took more then one second");
                        }
                    }
                }
                catch (Exception e)
                {
                    anotherThreadException = e;
                }
                finally
                {
                    secondLoggingSource.EndLogging();
                }
            });

            anotherThread.Start();
            anotherThread.Join();
            if (anotherThreadException != null)
            {
                throw anotherThreadException;
            }

            foreach (var file in beforeRestartFiles.OrderBy(f => f).SkipLast(1)) //The last is skipped because it is still written
            {
                var lastWriteTime = File.GetLastWriteTime(file);
                Assert.True(
                    restartDateTime > lastWriteTime,
                    $"{file} was changed (time:" +
                    $"{lastWriteTime.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)}) after the restart (time:" +
                    $"{restartDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)})" +
                    Environment.NewLine +
                    JustFileNamesAsString(beforeRestartFiles));
            }
        }
예제 #27
0
        public async Task LoggingSource_WhileRetentionByTimeOn_ShouldKeepRetentionPolicy(bool compressing)
        {
            const int fileSize = Constants.Size.Kilobyte;

            var name = GetTestName();
            var path = NewDataPath(forceCreateDir: true);

            path = Path.Combine(path, Guid.NewGuid().ToString());
            Directory.CreateDirectory(path);
            var retentionTime = TimeSpan.FromDays(3);

            var retentionDate   = DateTime.Now.Date - retentionTime;
            var toCheckLogFiles = new List <(string, bool)>();

            for (var date = retentionDate - TimeSpan.FromDays(1); date <= retentionDate + TimeSpan.FromDays(1); date += TimeSpan.FromDays(1))
            {
                var fileName = Path.Combine(path, LoggingSource.LogInfo.GetFileName(date) + ".001.log");
                toCheckLogFiles.Add((fileName, date >= retentionDate));
                var file = File.Create(fileName);
                file.Dispose();
            }

            var retentionSize = long.MaxValue;
            var loggingSource = new LoggingSource(
                LogMode.Information,
                path,
                "LoggingSource" + name,
                retentionTime,
                retentionSize,
                compressing);

            loggingSource.MaxFileSizeInBytes = fileSize;
            //This is just to make sure the MaxFileSizeInBytes is get action for the first file
            loggingSource.SetupLogMode(LogMode.Operations, path, retentionTime, retentionSize, compressing);

            var logger = new Logger(loggingSource, "Source" + name, "Logger" + name);

            for (var j = 0; j < 50; j++)
            {
                for (var i = 0; i < 5; i++)
                {
                    await logger.OperationsAsync("Some message");
                }
                Thread.Sleep(10);
            }

            loggingSource.EndLogging();

            var afterEndFiles = Directory.GetFiles(path);

            AssertNoFileMissing(afterEndFiles);

            try
            {
                foreach (var(fileName, shouldExist) in toCheckLogFiles)
                {
                    var compressedFileName = fileName + ".gz";
                    if (shouldExist)
                    {
                        Assert.True(afterEndFiles.Contains(fileName) || afterEndFiles.Contains(compressedFileName),
                                    $"The log file \"{Path.GetFileNameWithoutExtension(fileName)}\" and all log files from and after {retentionDate} " +
                                    "should be deleted due to time retention");
                    }
                    else
                    {
                        Assert.False(afterEndFiles.Contains(fileName),
                                     $"The file \"{fileName}\" and all log files from before {retentionDate} should be deleted due to time retention");

                        Assert.False(afterEndFiles.Contains(compressedFileName),
                                     $"The file \"{compressedFileName}\" and all log files from before {retentionDate} should be deleted due to time retention");
                    }
                }
            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"Logs after end - {JustFileNamesAsString(afterEndFiles)}", e);
            }
        }
예제 #28
0
        public async Task Register_WhenLogModeIsOperations_ShouldWriteToLogFileJustAsLogMode(LogMode logMode)
        {
            var timeout = TimeSpan.FromSeconds(10);

            var name = GetTestName();
            var path = NewDataPath(forceCreateDir: true);

            path = Path.Combine(path, Guid.NewGuid().ToString());
            Directory.CreateDirectory(path);

            var loggingSource = new LoggingSource(
                logMode,
                path,
                "LoggingSource" + name,
                TimeSpan.MaxValue,
                long.MaxValue,
                false);

            var logger = new Logger(loggingSource, "Source" + name, "Logger" + name);
            var tcs    = new TaskCompletionSource <WebSocketReceiveResult>(TaskCreationOptions.RunContinuationsAsynchronously);
            var socket = new DummyWebSocket();

            socket.ReceiveAsyncFunc = () => tcs.Task;
            var context = new LoggingSource.WebSocketContext();

            //Register
            var _ = loggingSource.Register(socket, context, CancellationToken.None);
            var beforeCloseOperation   = Guid.NewGuid().ToString();
            var beforeCloseInformation = Guid.NewGuid().ToString();

            var logTasks = Task.WhenAll(logger.OperationsAsync(beforeCloseOperation), logger.InfoAsync(beforeCloseInformation));
            await Task.WhenAny(logTasks, Task.Delay(timeout));

            Assert.True(logTasks.IsCompleted, $"Waited over {timeout.TotalSeconds} seconds for log tasks to finish");

            const int socketTimeout      = 5000;
            var       socketContainsLogs = WaitForValue(() => socket.LogsReceived.Contains(beforeCloseInformation) && socket.LogsReceived.Contains(beforeCloseOperation),
                                                        true, socketTimeout, 100);

            Assert.True(socketContainsLogs, $"Waited over {socketTimeout} seconds for log to be written to socket");

            //Close socket
            socket.Close();
            tcs.SetResult(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true, WebSocketCloseStatus.NormalClosure, string.Empty));

            var afterCloseOperation   = Guid.NewGuid().ToString();
            var afterCloseInformation = Guid.NewGuid().ToString();

            logTasks = Task.WhenAll(logger.OperationsAsync(afterCloseOperation), logger.InfoAsync(afterCloseInformation));
            await Task.WhenAny(logTasks, Task.Delay(timeout));

            Assert.True(logTasks.IsCompleted || logMode == LogMode.None,
                        $"Waited over {timeout.TotalSeconds} seconds for log tasks to finish");

            loggingSource.EndLogging();

            string logsFileContentAfter = await ReadLogsFileContent(path);

            AssertContainsLog(LogMode.Information, logMode)(beforeCloseInformation, logsFileContentAfter);
            AssertContainsLog(LogMode.Operations, logMode)(beforeCloseOperation, logsFileContentAfter);

            AssertContainsLog(LogMode.Information, logMode)(afterCloseInformation, logsFileContentAfter);
            AssertContainsLog(LogMode.Operations, logMode)(afterCloseOperation, logsFileContentAfter);
        }