Lean  $LEAN_TAG$
CompositeLogHandler.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 
19 {
20  /// <summary>
21  /// Provides an <see cref="ILogHandler"/> implementation that composes multiple handlers
22  /// </summary>
24  {
25  private readonly ILogHandler[] _handlers;
26 
27  /// <summary>
28  /// Initializes a new instance of the <see cref="CompositeLogHandler"/> that pipes log messages to the console and log.txt
29  /// </summary>
31  : this(new ConsoleLogHandler(), new FileLogHandler())
32  {
33  }
34 
35  /// <summary>
36  /// Initializes a new instance of the <see cref="CompositeLogHandler"/> class from the specified handlers
37  /// </summary>
38  /// <param name="handlers">The implementations to compose</param>
39  public CompositeLogHandler(params ILogHandler[] handlers)
40  {
41  if (handlers == null || handlers.Length == 0)
42  {
43  throw new ArgumentNullException(nameof(handlers));
44  }
45 
46  _handlers = handlers;
47  }
48 
49  /// <summary>
50  /// Write error message to log
51  /// </summary>
52  /// <param name="text"></param>
53  public void Error(string text)
54  {
55  foreach (var handler in _handlers)
56  {
57  handler.Error(text);
58  }
59  }
60 
61  /// <summary>
62  /// Write debug message to log
63  /// </summary>
64  /// <param name="text"></param>
65  public void Debug(string text)
66  {
67  foreach (var handler in _handlers)
68  {
69  handler.Debug(text);
70  }
71  }
72 
73  /// <summary>
74  /// Write debug message to log
75  /// </summary>
76  /// <param name="text"></param>
77  public void Trace(string text)
78  {
79  foreach (var handler in _handlers)
80  {
81  handler.Trace(text);
82  }
83  }
84 
85  /// <summary>
86  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
87  /// </summary>
88  /// <filterpriority>2</filterpriority>
89  public void Dispose()
90  {
91  foreach (var handler in _handlers)
92  {
93  handler.Dispose();
94  }
95  }
96  }
97 }