Trace the sql print message in .net code

來源:互聯網
上載者:User
The scenario is we usually print some message in the sql PROCEDURE to help us debug the PROCEDURE, but some time once we delopy the database we still want to trace the print message from the sql PROCEDURE to help us debug some bugs. This article will give a intrudce to implemented this scenarios.    1. First we create a sql procedure for test Create PROCEDURE dbo.TestPrintMessage as Print 'Warning: This is a warning message test' Print 'Error: This is a error message ' Print 'Critical: This is critical message' Print 'Verbose: This is verbose message' Print 'Information: This is information message' select 'test'    2. Use the C# code to call the sql procedure and trace these pring messages SqlConnection sqlCon = new SqlConnection("Data Source=.;Initial Catalog=AdventureWorks;Integrated Security=True"); sqlCon.InfoMessage += new SqlInfoMessageEventHandler(OnReceivingInfoMessageFromSql); SqlCommand cmd = new SqlCommand("dbo.TestPrintMessage"); cmd.CommandType = CommandType.StoredProcedure;  

cmd.ExecuteNonQuery();

3. Code for OnReceivingInfoMessageFromSql /// <summary> /// The event handler for the InfoMessage event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The event arguments.</param> private static void OnReceivingInfoMessageFromSql(object sender, SqlInfoMessageEventArgs e) { string prevLogMessage = String.Empty; Regex traceLevelPattern = new Regex(@"^(Warning|Error|Critical|Verbose|Information):",RegexOptions.IgnoreCase); TraceSource tracer = new TraceSource("DBLayer"); foreach (SqlError err in e.Errors) {     string errMessage = err.Message;     string logMessage = errMessage;     TraceEventType level = TraceEventType.Verbose;     Match m = traceLevelPattern.Match(errMessage);       if (m.Success)         {

            try

            {                level = (TraceEventType)Enum.Parse(                 typeof(TraceEventType), m.Result("$1"));            }           catch (ArgumentException ae)           {              tracer.TraceEvent(                  TraceEventType.Warning,                  0,               "Unexpected failure to parse the message trace event type. {0}",                ae.ToString());         }
   logMessage = errMessage.Substring(m.Result("$1:").Length);
   }
if (prevLogMessage != logMessage) { tracer.TraceEvent(level,0,"{0}@{1}: {2}",err.Procedure,err.LineNumber,logMessage); prevLogMessage = logMessage; }
   } }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.