OSDN Git Service

e4be2505bf35f173ea0ae09e1e7a96c8b39f8a2f
[dtxmania/dtxmania.git] / FDK / コード / 00.共通 / CTraceLogListener.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Diagnostics;
6
7 namespace FDK
8 {
9         public class CTraceLogListener : TraceListener
10         {
11                 public CTraceLogListener( StreamWriter stream )
12                 {
13                         this.streamWriter = stream;
14                 }
15
16                 public override void Flush()
17                 {
18                         if( this.streamWriter != null )
19                         {
20                                 try
21                                 {
22                                         this.streamWriter.Flush();
23                                 }
24                                 catch( ObjectDisposedException )
25                                 {
26                                 }
27                         }
28                 }
29                 public override void TraceEvent( TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message )
30                 {
31                         if( this.streamWriter != null )
32                         {
33                                 try
34                                 {
35                                         this.tイベント種別を出力する( eventType );
36                                         this.tインデントを出力する();
37                                         SanitizeUsername( ref message );
38                                         this.streamWriter.WriteLine( message );
39                                 }
40                                 catch( ObjectDisposedException )
41                                 {
42                                 }
43                         }
44                 }
45                 public override void TraceEvent( TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args )
46                 {
47                         if( this.streamWriter != null )
48                         {
49                                 try
50                                 {
51                                         this.tイベント種別を出力する( eventType );
52                                         this.tインデントを出力する();
53                                         string message = string.Format(format, args);
54                                         SanitizeUsername( ref message );
55                                         this.streamWriter.WriteLine( message );
56                                 }
57                                 catch( ObjectDisposedException )
58                                 {
59                                 }
60                         }
61                 }
62                 public override void Write( string message )
63                 {
64                         if( this.streamWriter != null )
65                         {
66                                 try
67                                 {
68                                         this.streamWriter.Write( message );
69                                 }
70                                 catch( ObjectDisposedException )
71                                 {
72                                 }
73                         }
74                 }
75                 public override void WriteLine( string message )
76                 {
77                         if( this.streamWriter != null )
78                         {
79                                 try
80                                 {
81                                         this.streamWriter.WriteLine( message );
82                                 }
83                                 catch( ObjectDisposedException )
84                                 {
85                                 }
86                         }
87                 }
88                 private string SanitizeUsername(ref string message)
89                 {
90                         string userprofile = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
91
92                         // もしユーザー名の情報が出力に存在する場合は、伏字にする
93                         if (message.Contains(userprofile))
94                         {
95                                 char delimiter = System.IO.Path.DirectorySeparatorChar;
96                                 string[] u = userprofile.Split(delimiter);
97                                 int c = u[u.Length - 1].Length;     // ユーザー名の文字数
98                                 u[u.Length - 1] = "*".PadRight(c, '*');
99                                 string sanitizedusername = string.Join(delimiter.ToString(), u);
100                                 message = message.Replace(userprofile, sanitizedusername);
101                                 return message;
102                         }
103                         return message;
104                 }
105
106                 protected override void Dispose( bool disposing )
107                 {
108                         if( this.streamWriter != null )
109                         {
110                                 try
111                                 {
112                                         this.streamWriter.Close();
113                                 }
114                                 catch
115                                 {
116                                 }
117                                 this.streamWriter = null;
118                         }
119                         base.Dispose( disposing );
120                 }
121
122                 #region [ private ]
123                 //-----------------
124                 private StreamWriter streamWriter;
125
126                 private void tイベント種別を出力する( TraceEventType eventType )
127                 {
128                         if( this.streamWriter != null )
129                         {
130                                 try
131                                 {
132                                         var now = DateTime.Now;
133                                         this.streamWriter.Write( string.Format( "{0:D4}/{1:D2}/{2:D2} {3:D2}:{4:D2}:{5:D2}.{6:D3} ", new object[] { now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond } ) );
134                                         switch( eventType )
135                                         {
136                                                 case TraceEventType.Error:
137                                                         this.streamWriter.Write( "[ERROR] " );
138                                                         return;
139
140                                                 case ( TraceEventType.Error | TraceEventType.Critical ):
141                                                         return;
142
143                                                 case TraceEventType.Warning:
144                                                         this.streamWriter.Write( "[WARNING] " );
145                                                         return;
146
147                                                 case TraceEventType.Information:
148                                                         break;
149
150                                                 default:
151                                                         return;
152                                         }
153                                         this.streamWriter.Write( "[INFO] " );
154                                 }
155                                 catch( ObjectDisposedException )
156                                 {
157                                 }
158                         }
159                 }
160                 private void tインデントを出力する()
161                 {
162                         if( ( this.streamWriter != null ) && ( base.IndentLevel > 0 ) )
163                         {
164                                 try
165                                 {
166                                         for( int i = 0; i < base.IndentLevel; i++ )
167                                                 this.streamWriter.Write( "    " );
168                                 }
169                                 catch( ObjectDisposedException )
170                                 {
171                                 }
172                         }
173                 }
174                 //-----------------
175                 #endregion
176         }
177 }