This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Log4NetInitializer.cs
98 lines (86 loc) · 3.61 KB
/
Log4NetInitializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//-----------------------------------------------------------------------
// <copyright file="Log4NetInitializer.cs" company="Copacetic Software">
// Copyright (c) Copacetic Software.
// <author>Philip Pittle</author>
// <date>Wednesday, April 30, 2014 5:48:10 PM</date>
// Licensed under the Apache License, Version 2.0,
// you may not use this file except in compliance with this License.
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
using System;
using CopaceticSoftware.CodeGenerator.StarterKit.Infrastructure;
using CopaceticSoftware.Common.Patterns;
using ICSharpCode.NRefactory.Utils;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
namespace CopaceticSoftware.CodeGenerator.StarterKit.Logging
{
public static class Log4NetInitializer
{
private static bool _isInitialized;
private static object _lock = new object();
public static void Initialize(IVisualStudioWriter visualStudioWriter, IServiceProvider serviceProvider)
{
if (_isInitialized)
return;
lock (_lock)
{
if (_isInitialized)
return;
//http://stackoverflow.com/questions/650694/changing-the-log-level-programmaticaly-in-log4net
var outputWindowAppender =
new VisualStudioOutputWindowAppender(visualStudioWriter)
{
Layout =
new PatternLayout(@"%date{HH:mm:ss,fff} %thread% %-5level [%logger{2}] %message%newline"),
Threshold =
#if DEBUG
Level.Debug
#else
Level.Info
#endif
};
outputWindowAppender.AddFilter(
new LevelAndLoggerFilter
{
LevelMax = Level.Fatal,
LevelMin = Level.Warn,
LoggerToMatch = typeof(IPipelineStep<>).Namespace
});
var activityLogAppender = new VisualStudioActivityLogAppender(serviceProvider)
{
Layout =
new PatternLayout(@"%date{HH:mm:ss,fff} %thread% %-5level [%logger{2}] %message%newline"),
Threshold = Level.Info
};
#if DEBUG
var debugFileAppender = new RollingFileAppender
{
AppendToFile = true,
Threshold = Level.All,
Layout = new PatternLayout(@"%date{HH:mm:ss,fff} %thread% %-5level [%logger{2}] %message%newline"),
File = @"c:\temp\pMixinsCodeGenerator.log",
DatePattern = "yyyyMMdd'.log'",
RollingStyle = RollingFileAppender.RollingMode.Date
};
#endif
log4net.Config.BasicConfigurator.Configure(
outputWindowAppender,
#if DEBUG
debugFileAppender,
#endif
activityLogAppender);
_isInitialized = true;
}
}
}
}