Пример #1
0
        /// <summary>
        /// Initializes the specified event source.
        /// </summary>
        /// <param name="eventSource">The event source.</param>
        public override void Initialize(IEventSource eventSource)
        {
            // get log file path from incoming parameters
            var logFile = GetLogFilePath(this.Parameters);


#if DEBUG
            this.diagnostics = DiagnosticsProvider.GetLogger();
            this.diagnostics.WriteMessage($"Parameters: {this.Parameters}");
            this.diagnostics.WriteMessage($"Verbosity level: {this.Verbosity}");
#endif

            try
            {
                // open the file
                this.streamWriter = new StreamWriter(logFile);

                // start JSON logging
                this.BeginJsonArray();

                // initialize JSON.NET writer
                this.jsonWriter            = new JsonTextWriter(this.streamWriter);
                this.jsonWriter.Formatting = Formatting.Indented;

                // initialize serializer
                this.serializer = new JsonSerializer();
            }
            catch (Exception ex)
            {
#if DEBUG
                // let's look at it
                this.diagnostics.WriteException(ex);
#endif

                if
                (
                    ex is UnauthorizedAccessException ||
                    ex is ArgumentNullException ||
                    ex is PathTooLongException ||
                    ex is DirectoryNotFoundException ||
                    ex is NotSupportedException ||
                    ex is ArgumentException ||
                    ex is SecurityException ||
                    ex is IOException
                )
                {
                    throw new LoggerException("Failed to create log file: " + ex.Message);
                }
                else
                {
                    // Unexpected failure
                    throw;
                }
            }

            // Occurs when a build raises any other type of build event.
            // Not sensitive to chosen MsBuild Verbosity level
            // Default logger behavior
            eventSource.AnyEventRaised += this.HandleEventSourceEvent;
        }