public void WritingToANullWriterThrows()
 {
     var formatter = new EventTextFormatter();
     formatter.WriteEvent(new EventEntry(Guid.NewGuid(), 0, string.Empty, new System.Collections.ObjectModel.ReadOnlyCollection<object>(new object[0]), DateTimeOffset.MaxValue, new EventSourceSchemaReader().GetSchema(Logger).Values.First()), null);
 }
 public void WritingNullEntryThrows()
 {
     var formatter = new EventTextFormatter();
     using (var writer = new StringWriter())
     {
         formatter.WriteEvent(null, writer);
     }
 }
        public void WritesNonEmptyRelatedActivityIdOnSummary()
        {
            var formatter = new EventTextFormatter(verbosityThreshold: EventLevel.Critical);
            using (var writer = new StringWriter())
            {
                var relatedActivityId = Guid.NewGuid();

                formatter.WriteEvent(
                    new EventEntry(
                        Guid.NewGuid(),
                        0,
                        string.Empty,
                        new System.Collections.ObjectModel.ReadOnlyCollection<object>(new object[0]),
                        DateTimeOffset.MaxValue,
                        Guid.Empty,
                        relatedActivityId,
                        new EventSourceSchemaReader().GetSchema(Logger).Values.First()),
                    writer);

                var message = writer.ToString();

                var activityIdMatch = Regex.Match(message, "\\bActivityId : (?<id>[-A-Fa-f0-9]+)\\w?").Groups["id"].Value;
                var relatedActivityIdMatch = Regex.Match(message, "\\bRelatedActivityId : (?<id>[-A-Fa-f0-9]+)\\w?").Groups["id"].Value;

                Assert.AreEqual(string.Empty, activityIdMatch);
                Assert.AreEqual(relatedActivityId.ToString(), relatedActivityIdMatch);
            }
        }
        public void WritesProcessIdAndThreadIdOnSummary()
        {
            var formatter = new EventTextFormatter(verbosityThreshold: EventLevel.Critical);
            using (var writer = new StringWriter())
            {
                var processId = 200;
                var threadId = 300;

                formatter.WriteEvent(
                    new EventEntry(
                        Guid.NewGuid(),
                        0,
                        string.Empty,
                        new System.Collections.ObjectModel.ReadOnlyCollection<object>(new object[0]),
                        DateTimeOffset.MaxValue,
                        processId,
                        threadId,
                        Guid.Empty,
                        Guid.Empty,
                        new EventSourceSchemaReader().GetSchema(Logger).Values.First()),
                    writer);

                var message = writer.ToString();

                var processIdMatch = Regex.Match(message, "\\bProcessId : (?<id>\\d+)\\w?").Groups["id"].Value;
                var threadIdMatch = Regex.Match(message, "\\bThreadId : (?<id>\\d+)\\w?").Groups["id"].Value;

                Assert.AreEqual(processId.ToString(), processIdMatch);
                Assert.AreEqual(threadId.ToString(), threadIdMatch);
            }
        }
        protected override void InitializeLocalLogging()
        {
            _platformEventStream = new ObservableEventListener();
            _platformEventStream.EnableEvents(EventSources.PlatformSources, TraceLevel);

            var formatter = new EventTextFormatter(dateTimeFormat: "O");
            _platformEventStream.Subscribe(evt =>
            {
                StringBuilder b = new StringBuilder();
                using (var writer = new StringWriter(b))
                {
                    formatter.WriteEvent(evt, writer);
                }
                Trace.WriteLine(b.ToString());
            });

            try
            {
                var logsResource = RoleEnvironment.GetLocalResource("Logs");

                var logFile = Path.Combine(logsResource.RootPath, "Platform", "Platform.log.json");

                // Initialize core platform logging
                _subscriptions.Add(_platformEventStream.LogToRollingFlatFile(
                    fileName: logFile,
                    rollSizeKB: 1024,
                    timestampPattern: "yyyyMMdd-HHmmss",
                    rollFileExistsBehavior: RollFileExistsBehavior.Increment,
                    rollInterval: RollInterval.Hour,
                    formatter: new JsonEventTextFormatter(EventTextFormatting.Indented, dateTimeFormat: "O"),
                    maxArchivedFiles: 768, // We have a buffer size of 1024MB for this folder
                    isAsync: false));
            }
            catch (Exception ex)
            {
                ServicePlatformEventSource.Log.FatalException(ex);
                throw;
            }
        }