PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Katmai_June_CTP/SMO/SmoEvents/CS/SmoEvents/SmoEvents.cs

#
C# | 101 lines | 63 code | 13 blank | 25 comment | 2 complexity | 5e4bd27c4573a2d2bc37ea32a44aa7fc MD5 | raw file
  1. /*=====================================================================
  2. File: SmoEvents.cs
  3. Summary: Monitor login attempts and database create and drop
  4. Date: February 17, 2005
  5. ---------------------------------------------------------------------
  6. This file is part of the Microsoft SQL Server Code Samples.
  7. Copyright (C) Microsoft Corporation. All rights reserved.
  8. This source code is intended only as a supplement to Microsoft
  9. Development Tools and/or on-line documentation. See these other
  10. materials for detailed information regarding Microsoft code samples.
  11. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  12. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  14. PARTICULAR PURPOSE.
  15. ======================================================= */
  16. #region Using directives
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. using Microsoft.SqlServer.Management.Common;
  21. using Microsoft.SqlServer.Management.Smo;
  22. using System.Threading;
  23. #endregion
  24. namespace Microsoft.Samples.SqlServer
  25. {
  26. class SmoEvents
  27. {
  28. static void Main()
  29. {
  30. // Default server is the local machine
  31. Server server = new Server();
  32. server.Events.ServerEvent
  33. += new ServerEventHandler(Events_ServerEvent);
  34. // Trace events
  35. server.Events.SubscribeToEvents(
  36. ServerTraceEvent.AuditLogin
  37. + ServerTraceEvent.AuditLogout
  38. + ServerTraceEvent.AuditLoginFailed);
  39. // DDL events
  40. server.Events.SubscribeToEvents(ServerEvent.CreateDatabase
  41. + ServerEvent.DropDatabase);
  42. Console.WriteLine(@"Starting events for server: {0}", server.Name);
  43. Console.WriteLine();
  44. Console.WriteLine(@"(Hit Ctrl-C or Ctrl-Break to quit.)");
  45. Console.WriteLine(@"Press any key to continue.");
  46. Console.ReadKey();
  47. // Start receiving events
  48. server.Events.StartEvents();
  49. // Wait indefinitely for events to occur
  50. Thread.Sleep(Timeout.Infinite);
  51. // Unsubscribe from all the events when finished
  52. server.Events.UnsubscribeAllEvents();
  53. }
  54. static void Events_ServerEvent(object sender, ServerEventArgs e)
  55. {
  56. try
  57. {
  58. Console.WriteLine(
  59. @"EventType: {0, -20} SPID: {1, 4} PostTime: {2, -20}",
  60. e.EventType, e.Spid, e.PostTime);
  61. foreach (EventProperty eventProperty in e.Properties)
  62. {
  63. if (eventProperty.Value != null)
  64. {
  65. Console.WriteLine("\t{0, -30} {1, -30}",
  66. eventProperty.Name,
  67. eventProperty.Value.ToString());
  68. }
  69. else
  70. {
  71. Console.WriteLine("\t{0,-30}", eventProperty.Name);
  72. }
  73. }
  74. Console.WriteLine();
  75. Console.WriteLine(new String('=', 79));
  76. Console.WriteLine();
  77. }
  78. catch (ApplicationException ex)
  79. {
  80. Console.WriteLine(ex.ToString());
  81. }
  82. }
  83. }
  84. }