NetSendAppender.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. // MONO 1.0 Beta mcs does not like #if !A && !B && !C syntax
  20. // .NET Compact Framework 1.0 has no support for Win32 NetMessageBufferSend API
  21. #if !NETCF
  22. // MONO 1.0 has no support for Win32 NetMessageBufferSend API
  23. #if !MONO
  24. // SSCLI 1.0 has no support for Win32 NetMessageBufferSend API
  25. #if !SSCLI
  26. // We don't want framework or platform specific code in the CLI version of log4net
  27. #if !CLI_1_0
  28. using System;
  29. using System.Globalization;
  30. using System.Runtime.InteropServices;
  31. using log4net.Util;
  32. using log4net.Layout;
  33. using log4net.Core;
  34. namespace log4net.Appender
  35. {
  36. /// <summary>
  37. /// Logs entries by sending network messages using the
  38. /// <see cref="NetMessageBufferSend" /> native function.
  39. /// </summary>
  40. /// <remarks>
  41. /// <para>
  42. /// You can send messages only to names that are active
  43. /// on the network. If you send the message to a user name,
  44. /// that user must be logged on and running the Messenger
  45. /// service to receive the message.
  46. /// </para>
  47. /// <para>
  48. /// The receiver will get a top most window displaying the
  49. /// messages one at a time, therefore this appender should
  50. /// not be used to deliver a high volume of messages.
  51. /// </para>
  52. /// <para>
  53. /// The following table lists some possible uses for this appender :
  54. /// </para>
  55. /// <para>
  56. /// <list type="table">
  57. /// <listheader>
  58. /// <term>Action</term>
  59. /// <description>Property Value(s)</description>
  60. /// </listheader>
  61. /// <item>
  62. /// <term>Send a message to a user account on the local machine</term>
  63. /// <description>
  64. /// <para>
  65. /// <see cref="NetSendAppender.Server"/> = &lt;name of the local machine&gt;
  66. /// </para>
  67. /// <para>
  68. /// <see cref="NetSendAppender.Recipient"/> = &lt;user name&gt;
  69. /// </para>
  70. /// </description>
  71. /// </item>
  72. /// <item>
  73. /// <term>Send a message to a user account on a remote machine</term>
  74. /// <description>
  75. /// <para>
  76. /// <see cref="NetSendAppender.Server"/> = &lt;name of the remote machine&gt;
  77. /// </para>
  78. /// <para>
  79. /// <see cref="NetSendAppender.Recipient"/> = &lt;user name&gt;
  80. /// </para>
  81. /// </description>
  82. /// </item>
  83. /// <item>
  84. /// <term>Send a message to a domain user account</term>
  85. /// <description>
  86. /// <para>
  87. /// <see cref="NetSendAppender.Server"/> = &lt;name of a domain controller | uninitialized&gt;
  88. /// </para>
  89. /// <para>
  90. /// <see cref="NetSendAppender.Recipient"/> = &lt;user name&gt;
  91. /// </para>
  92. /// </description>
  93. /// </item>
  94. /// <item>
  95. /// <term>Send a message to all the names in a workgroup or domain</term>
  96. /// <description>
  97. /// <para>
  98. /// <see cref="NetSendAppender.Recipient"/> = &lt;workgroup name | domain name&gt;*
  99. /// </para>
  100. /// </description>
  101. /// </item>
  102. /// <item>
  103. /// <term>Send a message from the local machine to a remote machine</term>
  104. /// <description>
  105. /// <para>
  106. /// <see cref="NetSendAppender.Server"/> = &lt;name of the local machine | uninitialized&gt;
  107. /// </para>
  108. /// <para>
  109. /// <see cref="NetSendAppender.Recipient"/> = &lt;name of the remote machine&gt;
  110. /// </para>
  111. /// </description>
  112. /// </item>
  113. /// </list>
  114. /// </para>
  115. /// <para>
  116. /// <b>Note :</b> security restrictions apply for sending
  117. /// network messages, see <see cref="NetMessageBufferSend" />
  118. /// for more information.
  119. /// </para>
  120. /// </remarks>
  121. /// <example>
  122. /// <para>
  123. /// An example configuration section to log information
  124. /// using this appender from the local machine, named
  125. /// LOCAL_PC, to machine OPERATOR_PC :
  126. /// </para>
  127. /// <code lang="XML" escaped="true">
  128. /// <appender name="NetSendAppender_Operator" type="log4net.Appender.NetSendAppender">
  129. /// <server value="LOCAL_PC" />
  130. /// <recipient value="OPERATOR_PC" />
  131. /// <layout type="log4net.Layout.PatternLayout" value="%-5p %c [%x] - %m%n" />
  132. /// </appender>
  133. /// </code>
  134. /// </example>
  135. /// <author>Nicko Cadell</author>
  136. /// <author>Gert Driesen</author>
  137. public class NetSendAppender : AppenderSkeleton
  138. {
  139. #region Member Variables
  140. /// <summary>
  141. /// The DNS or NetBIOS name of the server on which the function is to execute.
  142. /// </summary>
  143. private string m_server;
  144. /// <summary>
  145. /// The sender of the network message.
  146. /// </summary>
  147. private string m_sender;
  148. /// <summary>
  149. /// The message alias to which the message should be sent.
  150. /// </summary>
  151. private string m_recipient;
  152. /// <summary>
  153. /// The security context to use for privileged calls
  154. /// </summary>
  155. private SecurityContext m_securityContext;
  156. #endregion
  157. #region Constructors
  158. /// <summary>
  159. /// Initializes the appender.
  160. /// </summary>
  161. /// <remarks>
  162. /// The default constructor initializes all fields to their default values.
  163. /// </remarks>
  164. public NetSendAppender()
  165. {
  166. }
  167. #endregion
  168. #region Properties
  169. /// <summary>
  170. /// Gets or sets the sender of the message.
  171. /// </summary>
  172. /// <value>
  173. /// The sender of the message.
  174. /// </value>
  175. /// <remarks>
  176. /// If this property is not specified, the message is sent from the local computer.
  177. /// </remarks>
  178. public string Sender
  179. {
  180. get { return m_sender; }
  181. set { m_sender = value; }
  182. }
  183. /// <summary>
  184. /// Gets or sets the message alias to which the message should be sent.
  185. /// </summary>
  186. /// <value>
  187. /// The recipient of the message.
  188. /// </value>
  189. /// <remarks>
  190. /// This property should always be specified in order to send a message.
  191. /// </remarks>
  192. public string Recipient
  193. {
  194. get { return m_recipient; }
  195. set { m_recipient = value; }
  196. }
  197. /// <summary>
  198. /// Gets or sets the DNS or NetBIOS name of the remote server on which the function is to execute.
  199. /// </summary>
  200. /// <value>
  201. /// DNS or NetBIOS name of the remote server on which the function is to execute.
  202. /// </value>
  203. /// <remarks>
  204. /// <para>
  205. /// For Windows NT 4.0 and earlier, the string should begin with \\.
  206. /// </para>
  207. /// <para>
  208. /// If this property is not specified, the local computer is used.
  209. /// </para>
  210. /// </remarks>
  211. public string Server
  212. {
  213. get { return m_server; }
  214. set { m_server = value; }
  215. }
  216. /// <summary>
  217. /// Gets or sets the <see cref="SecurityContext"/> used to call the NetSend method.
  218. /// </summary>
  219. /// <value>
  220. /// The <see cref="SecurityContext"/> used to call the NetSend method.
  221. /// </value>
  222. /// <remarks>
  223. /// <para>
  224. /// Unless a <see cref="SecurityContext"/> specified here for this appender
  225. /// the <see cref="SecurityContextProvider.DefaultProvider"/> is queried for the
  226. /// security context to use. The default behavior is to use the security context
  227. /// of the current thread.
  228. /// </para>
  229. /// </remarks>
  230. public SecurityContext SecurityContext
  231. {
  232. get { return m_securityContext; }
  233. set { m_securityContext = value; }
  234. }
  235. #endregion
  236. #region Implementation of IOptionHandler
  237. /// <summary>
  238. /// Initialize the appender based on the options set.
  239. /// </summary>
  240. /// <remarks>
  241. /// <para>
  242. /// This is part of the <see cref="IOptionHandler"/> delayed object
  243. /// activation scheme. The <see cref="ActivateOptions"/> method must
  244. /// be called on this object after the configuration properties have
  245. /// been set. Until <see cref="ActivateOptions"/> is called this
  246. /// object is in an undefined state and must not be used.
  247. /// </para>
  248. /// <para>
  249. /// If any of the configuration properties are modified then
  250. /// <see cref="ActivateOptions"/> must be called again.
  251. /// </para>
  252. /// <para>
  253. /// The appender will be ignored if no <see cref="Recipient" /> was specified.
  254. /// </para>
  255. /// </remarks>
  256. /// <exception cref="ArgumentNullException">The required property <see cref="Recipient" /> was not specified.</exception>
  257. public override void ActivateOptions()
  258. {
  259. base.ActivateOptions();
  260. if (this.Recipient == null)
  261. {
  262. throw new ArgumentNullException("Recipient", "The required property 'Recipient' was not specified.");
  263. }
  264. if (m_securityContext == null)
  265. {
  266. m_securityContext = SecurityContextProvider.DefaultProvider.CreateSecurityContext(this);
  267. }
  268. }
  269. #endregion
  270. #region Override implementation of AppenderSkeleton
  271. /// <summary>
  272. /// This method is called by the <see cref="M:AppenderSkeleton.DoAppend(LoggingEvent)"/> method.
  273. /// </summary>
  274. /// <param name="loggingEvent">The event to log.</param>
  275. /// <remarks>
  276. /// <para>
  277. /// Sends the event using a network message.
  278. /// </para>
  279. /// </remarks>
  280. #if NET_4_0 || MONO_4_0 || NETSTANDARD1_3
  281. [System.Security.SecuritySafeCritical]
  282. #endif
  283. #if !NETSTANDARD1_3
  284. [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, UnmanagedCode = true)]
  285. #endif
  286. protected override void Append(LoggingEvent loggingEvent)
  287. {
  288. NativeError nativeError = null;
  289. // Render the event in the callers security context
  290. string renderedLoggingEvent = RenderLoggingEvent(loggingEvent);
  291. using(m_securityContext.Impersonate(this))
  292. {
  293. // Send the message
  294. int returnValue = NetMessageBufferSend(this.Server, this.Recipient, this.Sender, renderedLoggingEvent, renderedLoggingEvent.Length * Marshal.SystemDefaultCharSize);
  295. // Log the error if the message could not be sent
  296. if (returnValue != 0)
  297. {
  298. // Lookup the native error
  299. nativeError = NativeError.GetError(returnValue);
  300. }
  301. }
  302. if (nativeError != null)
  303. {
  304. // Handle the error over to the ErrorHandler
  305. ErrorHandler.Error(nativeError.ToString() + " (Params: Server=" + this.Server + ", Recipient=" + this.Recipient + ", Sender=" + this.Sender + ")");
  306. }
  307. }
  308. /// <summary>
  309. /// This appender requires a <see cref="Layout"/> to be set.
  310. /// </summary>
  311. /// <value><c>true</c></value>
  312. /// <remarks>
  313. /// <para>
  314. /// This appender requires a <see cref="Layout"/> to be set.
  315. /// </para>
  316. /// </remarks>
  317. override protected bool RequiresLayout
  318. {
  319. get { return true; }
  320. }
  321. #endregion
  322. #region Stubs For Native Function Calls
  323. /// <summary>
  324. /// Sends a buffer of information to a registered message alias.
  325. /// </summary>
  326. /// <param name="serverName">The DNS or NetBIOS name of the server on which the function is to execute.</param>
  327. /// <param name="msgName">The message alias to which the message buffer should be sent</param>
  328. /// <param name="fromName">The originator of the message.</param>
  329. /// <param name="buffer">The message text.</param>
  330. /// <param name="bufferSize">The length, in bytes, of the message text.</param>
  331. /// <remarks>
  332. /// <para>
  333. /// The following restrictions apply for sending network messages:
  334. /// </para>
  335. /// <para>
  336. /// <list type="table">
  337. /// <listheader>
  338. /// <term>Platform</term>
  339. /// <description>Requirements</description>
  340. /// </listheader>
  341. /// <item>
  342. /// <term>Windows NT</term>
  343. /// <description>
  344. /// <para>
  345. /// No special group membership is required to send a network message.
  346. /// </para>
  347. /// <para>
  348. /// Admin, Accounts, Print, or Server Operator group membership is required to
  349. /// successfully send a network message on a remote server.
  350. /// </para>
  351. /// </description>
  352. /// </item>
  353. /// <item>
  354. /// <term>Windows 2000 or later</term>
  355. /// <description>
  356. /// <para>
  357. /// If you send a message on a domain controller that is running Active Directory,
  358. /// access is allowed or denied based on the access control list (ACL) for the securable
  359. /// object. The default ACL permits only Domain Admins and Account Operators to send a network message.
  360. /// </para>
  361. /// <para>
  362. /// On a member server or workstation, only Administrators and Server Operators can send a network message.
  363. /// </para>
  364. /// </description>
  365. /// </item>
  366. /// </list>
  367. /// </para>
  368. /// <para>
  369. /// For more information see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/security_requirements_for_the_network_management_functions.asp">Security Requirements for the Network Management Functions</a>.
  370. /// </para>
  371. /// </remarks>
  372. /// <returns>
  373. /// <para>
  374. /// If the function succeeds, the return value is zero.
  375. /// </para>
  376. /// </returns>
  377. [DllImport("netapi32.dll", SetLastError=true)]
  378. protected static extern int NetMessageBufferSend(
  379. [MarshalAs(UnmanagedType.LPWStr)] string serverName,
  380. [MarshalAs(UnmanagedType.LPWStr)] string msgName,
  381. [MarshalAs(UnmanagedType.LPWStr)] string fromName,
  382. [MarshalAs(UnmanagedType.LPWStr)] string buffer,
  383. int bufferSize);
  384. #endregion
  385. }
  386. }
  387. #endif // !CLI_1_0
  388. #endif // !SSCLI
  389. #endif // !MONO
  390. #endif // !NETCF