OutputDebugStringAppender.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 has no support for Win32 OutputDebugString API
  20. #if !MONO
  21. // SSCLI 1.0 has no support for Win32 OutputDebugString API
  22. #if !SSCLI
  23. // We don't want framework or platform specific code in the CLI version of log4net
  24. #if !CLI_1_0
  25. using System.Runtime.InteropServices;
  26. using log4net.Layout;
  27. using log4net.Core;
  28. namespace log4net.Appender
  29. {
  30. /// <summary>
  31. /// Appends log events to the OutputDebugString system.
  32. /// </summary>
  33. /// <remarks>
  34. /// <para>
  35. /// OutputDebugStringAppender appends log events to the
  36. /// OutputDebugString system.
  37. /// </para>
  38. /// <para>
  39. /// The string is passed to the native <c>OutputDebugString</c>
  40. /// function.
  41. /// </para>
  42. /// </remarks>
  43. /// <author>Nicko Cadell</author>
  44. /// <author>Gert Driesen</author>
  45. public class OutputDebugStringAppender : AppenderSkeleton
  46. {
  47. #region Public Instance Constructors
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="OutputDebugStringAppender" /> class.
  50. /// </summary>
  51. /// <remarks>
  52. /// <para>
  53. /// Default constructor.
  54. /// </para>
  55. /// </remarks>
  56. public OutputDebugStringAppender()
  57. {
  58. }
  59. #endregion // Public Instance Constructors
  60. #region Override implementation of AppenderSkeleton
  61. /// <summary>
  62. /// Write the logging event to the output debug string API
  63. /// </summary>
  64. /// <param name="loggingEvent">the event to log</param>
  65. /// <remarks>
  66. /// <para>
  67. /// Write the logging event to the output debug string API
  68. /// </para>
  69. /// </remarks>
  70. #if NET_4_0 || MONO_4_0 || NETSTANDARD1_3
  71. [System.Security.SecuritySafeCritical]
  72. #elif !NETCF
  73. [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, UnmanagedCode = true)]
  74. #endif
  75. override protected void Append(LoggingEvent loggingEvent)
  76. {
  77. #if NETSTANDARD1_3
  78. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  79. {
  80. throw new System.PlatformNotSupportedException("OutputDebugString is only available on Windows");
  81. }
  82. #endif
  83. OutputDebugString(RenderLoggingEvent(loggingEvent));
  84. }
  85. /// <summary>
  86. /// This appender requires a <see cref="Layout"/> to be set.
  87. /// </summary>
  88. /// <value><c>true</c></value>
  89. /// <remarks>
  90. /// <para>
  91. /// This appender requires a <see cref="Layout"/> to be set.
  92. /// </para>
  93. /// </remarks>
  94. override protected bool RequiresLayout
  95. {
  96. get { return true; }
  97. }
  98. #endregion // Override implementation of AppenderSkeleton
  99. #region Protected Static Methods
  100. /// <summary>
  101. /// Stub for OutputDebugString native method
  102. /// </summary>
  103. /// <param name="message">the string to output</param>
  104. /// <remarks>
  105. /// <para>
  106. /// Stub for OutputDebugString native method
  107. /// </para>
  108. /// </remarks>
  109. #if NETCF
  110. [DllImport("CoreDll.dll")]
  111. #else
  112. [DllImport("Kernel32.dll")]
  113. #endif
  114. protected static extern void OutputDebugString(string message);
  115. #endregion // Protected Static Methods
  116. }
  117. }
  118. #endif // !CLI_1_0
  119. #endif // !SSCLI
  120. #endif // !MONO