zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
#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
 
// .NET Compact Framework 1.0 has no support for System.Runtime.Remoting
#if !NETCF
 
using System;
using System.Runtime.Remoting;
 
using log4net.Util;
using log4net.Repository;
using log4net.Core;
using IRemoteLoggingSink = log4net.Appender.RemotingAppender.IRemoteLoggingSink;
 
namespace log4net.Plugin
{
    /// <summary>
    /// Plugin that listens for events from the <see cref="log4net.Appender.RemotingAppender"/>
    /// </summary>
    /// <remarks>
    /// <para>
    /// This plugin publishes an instance of <see cref="IRemoteLoggingSink"/> 
    /// on a specified <see cref="SinkUri"/>. This listens for logging events delivered from
    /// a remote <see cref="log4net.Appender.RemotingAppender"/>.
    /// </para>
    /// <para>
    /// When an event is received it is relogged within the attached repository
    /// as if it had been raised locally.
    /// </para>
    /// </remarks>
    /// <author>Nicko Cadell</author>
    /// <author>Gert Driesen</author>
    public class RemoteLoggingServerPlugin : PluginSkeleton
    {
        #region Public Instance Constructors
 
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <remarks>
        /// <para>
        /// Initializes a new instance of the <see cref="RemoteLoggingServerPlugin" /> class.
        /// </para>
        /// <para>
        /// The <see cref="SinkUri"/> property must be set.
        /// </para>
        /// </remarks>
        public RemoteLoggingServerPlugin() : base("RemoteLoggingServerPlugin:Unset URI")
        {
        }
 
        /// <summary>
        /// Construct with sink Uri.
        /// </summary>
        /// <param name="sinkUri">The name to publish the sink under in the remoting infrastructure. 
        /// See <see cref="SinkUri"/> for more details.</param>
        /// <remarks>
        /// <para>
        /// Initializes a new instance of the <see cref="RemoteLoggingServerPlugin" /> class
        /// with specified name.
        /// </para>
        /// </remarks>
        public RemoteLoggingServerPlugin(string sinkUri) : base("RemoteLoggingServerPlugin:"+sinkUri)
        {
            m_sinkUri = sinkUri;
        }
 
        #endregion Public Instance Constructors
 
        #region Public Instance Properties
 
        /// <summary>
        /// Gets or sets the URI of this sink.
        /// </summary>
        /// <value>
        /// The URI of this sink.
        /// </value>
        /// <remarks>
        /// <para>
        /// This is the name under which the object is marshaled.
        /// <see cref="RemotingServices.Marshal(MarshalByRefObject,String,Type)"/>
        /// </para>
        /// </remarks>
        public virtual string SinkUri 
        { 
            get { return m_sinkUri; }
            set { m_sinkUri = value; }
        }
 
        #endregion Public Instance Properties
 
        #region Override implementation of PluginSkeleton
 
        /// <summary>
        /// Attaches this plugin to a <see cref="ILoggerRepository"/>.
        /// </summary>
        /// <param name="repository">The <see cref="ILoggerRepository"/> that this plugin should be attached to.</param>
        /// <remarks>
        /// <para>
        /// A plugin may only be attached to a single repository.
        /// </para>
        /// <para>
        /// This method is called when the plugin is attached to the repository.
        /// </para>
        /// </remarks>
        override public void Attach(ILoggerRepository repository)
        {
            base.Attach(repository);
 
            // Create the sink and marshal it
            m_sink = new RemoteLoggingSinkImpl(repository); 
 
            try
            {
                RemotingServices.Marshal(m_sink, m_sinkUri, typeof(IRemoteLoggingSink));        
            }
            catch(Exception ex)
            {
                LogLog.Error(declaringType, "Failed to Marshal remoting sink", ex);
            }
        }
 
        /// <summary>
        /// Is called when the plugin is to shutdown.
        /// </summary>
        /// <remarks>
        /// <para>
        /// When the plugin is shutdown the remote logging
        /// sink is disconnected.
        /// </para>
        /// </remarks>
#if NET_4_0
        [System.Security.SecuritySafeCritical]
#endif
        override public void Shutdown()
        {
            // Stops the sink from receiving messages
            RemotingServices.Disconnect(m_sink);
            m_sink = null;
 
            base.Shutdown();
        }
 
        #endregion Override implementation of PluginSkeleton
 
        #region Private Instance Fields
 
        private RemoteLoggingSinkImpl m_sink;
        private string m_sinkUri;
 
        #endregion Private Instance Fields
 
        #region Private Static Fields
 
        /// <summary>
        /// The fully qualified type of the RemoteLoggingServerPlugin class.
        /// </summary>
        /// <remarks>
        /// Used by the internal logger to record the Type of the
        /// log message.
        /// </remarks>
        private readonly static Type declaringType = typeof(RemoteLoggingServerPlugin);
 
        #endregion Private Static Fields
 
        /// <summary>
        /// Delivers <see cref="LoggingEvent"/> objects to a remote sink.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Internal class used to listen for logging events
        /// and deliver them to the local repository.
        /// </para>
        /// </remarks>
        private class RemoteLoggingSinkImpl : MarshalByRefObject, IRemoteLoggingSink
        {
            #region Public Instance Constructors
 
            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="repository">The repository to log to.</param>
            /// <remarks>
            /// <para>
            /// Initializes a new instance of the <see cref="RemoteLoggingSinkImpl"/> for the
            /// specified <see cref="ILoggerRepository"/>.
            /// </para>
            /// </remarks>
            public RemoteLoggingSinkImpl(ILoggerRepository repository)
            {
                m_repository = repository;
            }
 
            #endregion Public Instance Constructors
 
            #region Implementation of IRemoteLoggingSink
 
            /// <summary>
            /// Logs the events to the repository.
            /// </summary>
            /// <param name="events">The events to log.</param>
            /// <remarks>
            /// <para>
            /// The events passed are logged to the <see cref="ILoggerRepository"/>
            /// </para>
            /// </remarks>
            public void LogEvents(LoggingEvent[] events)
            {
                if (events != null)
                {
                    foreach(LoggingEvent logEvent in events)
                    {
                        if (logEvent != null)
                        {
                            m_repository.Log(logEvent);
                        }
                    }
                }
            }
 
            #endregion Implementation of IRemoteLoggingSink
 
            #region Override implementation of MarshalByRefObject
 
            /// <summary>
            /// Obtains a lifetime service object to control the lifetime 
            /// policy for this instance.
            /// </summary>
            /// <returns><c>null</c> to indicate that this instance should live forever.</returns>
            /// <remarks>
            /// <para>
            /// Obtains a lifetime service object to control the lifetime 
            /// policy for this instance. This object should live forever
            /// therefore this implementation returns <c>null</c>.
            /// </para>
            /// </remarks>
#if NET_4_0
            [System.Security.SecurityCritical]
#endif
            public override object InitializeLifetimeService()
            {
                return null;
            }
 
            #endregion Override implementation of MarshalByRefObject
 
            #region Private Instance Fields
 
            /// <summary>
            /// The underlying <see cref="ILoggerRepository" /> that events should
            /// be logged to.
            /// </summary>
            private readonly ILoggerRepository m_repository;
 
            #endregion Private Instance Fields
        }
    }
}
 
#endif // !NETCF