jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the 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.
//
#endregion
 
using System;
using System.Text;
using System.IO;
 
using log4net.Layout;
using log4net.Core;
using log4net.Util;
 
namespace log4net.Appender
{
    /// <summary>
    /// Send an email when a specific logging event occurs, typically on errors 
    /// or fatal errors. Rather than sending via smtp it writes a file into the
    /// directory specified by <see cref="PickupDir"/>. This allows services such
    /// as the IIS SMTP agent to manage sending the messages.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The configuration for this appender is identical to that of the <c>SMTPAppender</c>,
    /// except that instead of specifying the <c>SMTPAppender.SMTPHost</c> you specify
    /// <see cref="PickupDir"/>.
    /// </para>
    /// <para>
    /// The number of logging events delivered in this e-mail depend on
    /// the value of <see cref="BufferingAppenderSkeleton.BufferSize"/> option. The
    /// <see cref="SmtpPickupDirAppender"/> keeps only the last
    /// <see cref="BufferingAppenderSkeleton.BufferSize"/> logging events in its 
    /// cyclic buffer. This keeps memory requirements at a reasonable level while 
    /// still delivering useful application context.
    /// </para>
    /// </remarks>
    /// <author>Niall Daley</author>
    /// <author>Nicko Cadell</author>
    public class SmtpPickupDirAppender : BufferingAppenderSkeleton
    {
        #region Public Instance Constructors
 
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <remarks>
        /// <para>
        /// Default constructor
        /// </para>
        /// </remarks>
        public SmtpPickupDirAppender()
        {
        }
 
        #endregion Public Instance Constructors
 
        #region Public Instance Properties
 
        /// <summary>
        /// Gets or sets a semicolon-delimited list of recipient e-mail addresses.
        /// </summary>
        /// <value>
        /// A semicolon-delimited list of e-mail addresses.
        /// </value>
        /// <remarks>
        /// <para>
        /// A semicolon-delimited list of e-mail addresses.
        /// </para>
        /// </remarks>
        public string To 
        {
            get { return m_to; }
            set { m_to = value; }
        }
 
        /// <summary>
        /// Gets or sets the e-mail address of the sender.
        /// </summary>
        /// <value>
        /// The e-mail address of the sender.
        /// </value>
        /// <remarks>
        /// <para>
        /// The e-mail address of the sender.
        /// </para>
        /// </remarks>
        public string From 
        {
            get { return m_from; }
            set { m_from = value; }
        }
 
        /// <summary>
        /// Gets or sets the subject line of the e-mail message.
        /// </summary>
        /// <value>
        /// The subject line of the e-mail message.
        /// </value>
        /// <remarks>
        /// <para>
        /// The subject line of the e-mail message.
        /// </para>
        /// </remarks>
        public string Subject 
        {
            get { return m_subject; }
            set { m_subject = value; }
        }
  
        /// <summary>
        /// Gets or sets the path to write the messages to.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Gets or sets the path to write the messages to. This should be the same
        /// as that used by the agent sending the messages.
        /// </para>
        /// </remarks>
        public string PickupDir
        {
            get { return m_pickupDir; }
            set { m_pickupDir = value; }
        }
 
        /// <summary>
        /// Gets or sets the <see cref="SecurityContext"/> used to write to the pickup directory.
        /// </summary>
        /// <value>
        /// The <see cref="SecurityContext"/> used to write to the pickup directory.
        /// </value>
        /// <remarks>
        /// <para>
        /// Unless a <see cref="SecurityContext"/> specified here for this appender
        /// the <see cref="SecurityContextProvider.DefaultProvider"/> is queried for the
        /// security context to use. The default behavior is to use the security context
        /// of the current thread.
        /// </para>
        /// </remarks>
        public SecurityContext SecurityContext 
        {
            get { return m_securityContext; }
            set { m_securityContext = value; }
        }
 
        #endregion Public Instance Properties
 
        #region Override implementation of BufferingAppenderSkeleton
 
        /// <summary>
        /// Sends the contents of the cyclic buffer as an e-mail message.
        /// </summary>
        /// <param name="events">The logging events to send.</param>
        /// <remarks>
        /// <para>
        /// Sends the contents of the cyclic buffer as an e-mail message.
        /// </para>
        /// </remarks>
        override protected void SendBuffer(LoggingEvent[] events) 
        {
            // Note: this code already owns the monitor for this
            // appender. This frees us from needing to synchronize again.
            try 
            {
                string filePath = null;
                StreamWriter writer = null;
 
                // Impersonate to open the file
                using(SecurityContext.Impersonate(this))
                {
                    filePath = Path.Combine(m_pickupDir, SystemInfo.NewGuid().ToString("N"));
                    writer = File.CreateText(filePath);
                }
 
                if (writer == null)
                {
                    ErrorHandler.Error("Failed to create output file for writing ["+filePath+"]", null, ErrorCode.FileOpenFailure);
                }
                else
                {
                    using(writer)
                    {
                        writer.WriteLine("To: " + m_to);
                        writer.WriteLine("From: " + m_from);
                        writer.WriteLine("Subject: " + m_subject);
                        writer.WriteLine("");
 
                        string t = Layout.Header;
                        if (t != null)
                        {
                            writer.Write(t);
                        }
 
                        for(int i = 0; i < events.Length; i++) 
                        {
                            // Render the event and append the text to the buffer
                            RenderLoggingEvent(writer, events[i]);
                        }
 
                        t = Layout.Footer;
                        if (t != null)
                        {
                            writer.Write(t);
                        }
 
                        writer.WriteLine("");
                        writer.WriteLine(".");
                    }
                }
            } 
            catch(Exception e) 
            {
                ErrorHandler.Error("Error occurred while sending e-mail notification.", e);
            }
        }
 
        #endregion Override implementation of BufferingAppenderSkeleton
 
        #region Override implementation of AppenderSkeleton
 
        /// <summary>
        /// Activate the options on this appender. 
        /// </summary>
        /// <remarks>
        /// <para>
        /// This is part of the <see cref="IOptionHandler"/> delayed object
        /// activation scheme. The <see cref="ActivateOptions"/> method must 
        /// be called on this object after the configuration properties have
        /// been set. Until <see cref="ActivateOptions"/> is called this
        /// object is in an undefined state and must not be used. 
        /// </para>
        /// <para>
        /// If any of the configuration properties are modified then 
        /// <see cref="ActivateOptions"/> must be called again.
        /// </para>
        /// </remarks>
        override public void ActivateOptions() 
        {    
            base.ActivateOptions();
 
            if (m_securityContext == null)
            {
                m_securityContext = SecurityContextProvider.DefaultProvider.CreateSecurityContext(this);
            }
 
            using(SecurityContext.Impersonate(this))
            {
                m_pickupDir = ConvertToFullPath(m_pickupDir.Trim());
            }
        }
 
        /// <summary>
        /// This appender requires a <see cref="Layout"/> to be set.
        /// </summary>
        /// <value><c>true</c></value>
        /// <remarks>
        /// <para>
        /// This appender requires a <see cref="Layout"/> to be set.
        /// </para>
        /// </remarks>
        override protected bool RequiresLayout
        {
            get { return true; }
        }
 
        #endregion Override implementation of AppenderSkeleton
 
        #region Protected Static Methods
 
        /// <summary>
        /// Convert a path into a fully qualified path.
        /// </summary>
        /// <param name="path">The path to convert.</param>
        /// <returns>The fully qualified path.</returns>
        /// <remarks>
        /// <para>
        /// Converts the path specified to a fully
        /// qualified path. If the path is relative it is
        /// taken as relative from the application base 
        /// directory.
        /// </para>
        /// </remarks>
        protected static string ConvertToFullPath(string path)
        {
            return SystemInfo.ConvertToFullPath(path);
        }
 
        #endregion Protected Static Methods
 
        #region Private Instance Fields
 
        private string m_to;
        private string m_from;
        private string m_subject;
        private string m_pickupDir;
 
        /// <summary>
        /// The security context to use for privileged calls
        /// </summary>
        private SecurityContext m_securityContext;
 
        #endregion Private Instance Fields
    }
}