NTP Analyzer  0.8.2
Analyze the operation of time servers
LogFactory.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using Ntp.Common.System;
25 
26 namespace Ntp.Common.Log
27 {
28  public static class LogFactory
29  {
30  public static string ApplicationName;
31  private static readonly List<LogBase> Logs = new List<LogBase>();
32 
33  public static void Cleanup()
34  {
35  Close();
36  Logs.Clear();
37  }
38 
39  public static void Close()
40  {
41  foreach (var log in Logs)
42  log.Close();
43  }
44 
46  {
47  return new ActivityLog();
48  }
49 
50  public static LogGroup CreateErrorLog(string name)
51  {
52  var log = new LogGroup
53  {
54  new ConsoleLog(Severity.Error)
55  {
56  ShowSeverity = false,
57  ShowTimeStamp = false
58  }
59  };
60  return log;
61  }
62 
63  public static LogGroup CreateGroupLog()
64  {
65  return new LogGroup();
66  }
67 
68  public static LogBase CreateLog(ILogConfiguration config)
69  {
70  var log = CreateLogInternal(config);
71  Logs.Add(log);
72  return log;
73  }
74 
75  public static LogBase CreateLog(IEnumerable<ILogConfiguration> configs)
76  {
77  var group = new LogGroup();
78 
79  foreach (var config in configs)
80  {
81  var log = CreateLogInternal(config);
82  group.Add(log);
83  Logs.Add(log);
84  }
85 
86  return group;
87  }
88 
89  public static LogBase CreateSysLog(string name)
90  {
91  var log = ProcessInfo.CreateSystemLog(name);
92  Logs.Add(log);
93  return log;
94  }
95 
96  public static void Resume()
97  {
98  foreach (var log in Logs)
99  log.Resume();
100  }
101 
102  public static void Suspend()
103  {
104  foreach (var log in Logs)
105  log.Suspend();
106  }
107 
109  {
110  switch (config.LogType)
111  {
112  case LogType.Unknown:
113  case LogType.Console:
114  return new ConsoleLog(config.Threshold, config.TimeFormat)
115  {
116  ShowSeverity = config.ShowSeverity,
117  ShowTimeStamp = config.ShowTimestamp
118  };
119  case LogType.Syslog:
120  return new SysLog(ApplicationName, config.Threshold);
121  case LogType.EventLog:
122  return new EventLog(ApplicationName, config.Threshold);
123  case LogType.File:
124  return new FileLog(config.File, config.Threshold, config.TimeFormat)
125  {
126  ShowSeverity = config.ShowSeverity,
127  ShowTimeStamp = config.ShowTimestamp
128  };
129  default:
130  throw new ArgumentOutOfRangeException(nameof(config));
131  }
132  }
133  }
134 }
static LogBase CreateLog(ILogConfiguration config)
Definition: LogFactory.cs:68
static ActivityLog CreateActivityLog()
Definition: LogFactory.cs:45
static LogGroup CreateGroupLog()
Definition: LogFactory.cs:63
static void Cleanup()
Definition: LogFactory.cs:33
static LogBase CreateSysLog(string name)
Definition: LogFactory.cs:89
static LogBase CreateLogInternal(ILogConfiguration config)
Definition: LogFactory.cs:108
static LogBase CreateSystemLog(string name)
Definition: ProcessInfo.cs:34
static string ApplicationName
Definition: LogFactory.cs:30
static void Resume()
Definition: LogFactory.cs:96
static LogGroup CreateErrorLog(string name)
Definition: LogFactory.cs:50
static LogBase CreateLog(IEnumerable< ILogConfiguration > configs)
Definition: LogFactory.cs:75