DebugProvider.Windows.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Diagnostics
  5. {
  6. public partial class DebugProvider
  7. {
  8. public static void FailCore(string stackTrace, string? message, string? detailMessage, string errorSource)
  9. {
  10. if (s_FailCore != null)
  11. {
  12. s_FailCore(stackTrace, message, detailMessage, errorSource);
  13. return;
  14. }
  15. if (Debugger.IsAttached)
  16. {
  17. Debugger.Break();
  18. }
  19. else
  20. {
  21. // In Core, we do not show a dialog.
  22. // Fail in order to avoid anyone catching an exception and masking
  23. // an assert failure.
  24. DebugAssertException ex = new DebugAssertException(message, detailMessage, stackTrace);
  25. Environment.FailFast(ex.Message, ex, errorSource);
  26. }
  27. }
  28. private static readonly object s_ForLock = new object();
  29. public static void WriteCore(string message)
  30. {
  31. if (s_WriteCore != null)
  32. {
  33. s_WriteCore(message);
  34. return;
  35. }
  36. // really huge messages mess up both VS and dbmon, so we chop it up into
  37. // reasonable chunks if it's too big. This is the number of characters
  38. // that OutputDebugstring chunks at.
  39. const int WriteChunkLength = 4091;
  40. // We don't want output from multiple threads to be interleaved.
  41. lock (s_ForLock)
  42. {
  43. if (message.Length <= WriteChunkLength)
  44. {
  45. WriteToDebugger(message);
  46. }
  47. else
  48. {
  49. int offset;
  50. for (offset = 0; offset < message.Length - WriteChunkLength; offset += WriteChunkLength)
  51. {
  52. WriteToDebugger(message.Substring(offset, WriteChunkLength));
  53. }
  54. WriteToDebugger(message.Substring(offset));
  55. }
  56. }
  57. }
  58. private static void WriteToDebugger(string message)
  59. {
  60. if (Debugger.IsLogging())
  61. {
  62. Debugger.Log(0, null, message);
  63. }
  64. else
  65. {
  66. Interop.Kernel32.OutputDebugString(message ?? string.Empty);
  67. }
  68. }
  69. }
  70. }