Csharp/C Sharp/Development Class/Trace
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
demonstrates post-deployment tracing
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
#define TRACE
/*
Example21_13.cs demonstrates post-deployment tracing
*/
using System;
using System.Globalization;
using System.Diagnostics;
public class Example21_13
{
public static void Main()
{
// Set up a switch to turn tracing on
BooleanSwitch bsEnableTrace = new BooleanSwitch("TraceOutput",
"Turn on tracing");
// Set up a TraceListener to a file
TextWriterTraceListener tl = new TextWriterTraceListener("Example21_13.txt");
Trace.Listeners.Add(tl);
// And a second TraceListener to the event log
EventLogTraceListener t2 = new EventLogTraceListener("Application");
Trace.Listeners.Add(t2);
Trace.WriteLineIf(bsEnableTrace.Enabled,"Starting Main()");
// create a date and a currency value
DateTime dtNow = DateTime.Now;
Double curOriginal = 12345.67;
// and format the variables for a specific culture
CultureInfo ci = new CultureInfo("en-US");
string sLocalizedDate = dtNow.ToString("d", ci);
string sLocalizedCur = curOriginal.ToString("c", ci);
if (bsEnableTrace.Enabled)
{
Trace.Assert(sLocalizedDate != null, "Localized date has no content");
}
// print them out
Console.WriteLine(sLocalizedDate);
Console.WriteLine(sLocalizedCur);
Trace.WriteLineIf(bsEnableTrace.Enabled, "Exiting Main()");
Trace.Flush();
Trace.Close();
}
}
demonstrates the use of TraceListener objects
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
#define DEBUG
/*
Example21_12.cs demonstrates the use of TraceListener objects
*/
using System;
using System.Globalization;
using System.Diagnostics;
public class Example21_12
{
public static void Main()
{
// Set up a TraceListener to a file
TextWriterTraceListener tl = new TextWriterTraceListener("Example21_12.txt");
Debug.Listeners.Add(tl);
// And a second TraceListener to the event log
EventLogTraceListener t2 = new EventLogTraceListener("Application");
Debug.Listeners.Add(t2);
Debug.WriteLine("Starting Main()");
// create a date and a currency value
DateTime dtNow = DateTime.Now;
Double curOriginal = 12345.67;
// and format the variables for a specific culture
CultureInfo ci = new CultureInfo("en-US");
string sLocalizedDate = dtNow.ToString("d", ci);
string sLocalizedCur = curOriginal.ToString("c", ci);
Debug.Assert(sLocalizedDate != null, "Localized date has no content");
// print them out
Console.WriteLine(sLocalizedDate);
Console.WriteLine(sLocalizedCur);
Debug.WriteLine("Exiting Main()");
Debug.Flush();
Debug.Close();
}
}
Trace.WriteLine
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
class MainClass {
static void Main() {
Trace.WriteLine("Entered Main()");
for (int i = 0; i < 6; i++)
Trace.WriteLine(i);
Trace.WriteLine("Exiting from Main()");
}
}
Use Trace.Fail to alert a fail
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
class Class1 {
[STAThread]
static void Main(string[] args) {
SqlConnection dbConn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
SqlCommand dbComm = new SqlCommand("SELECT * FROM " + "authors", dbConn);
SqlDataReader dr = null;
Trace.WriteLine(DateTime.Now + " - Executing SQL statement");
try {
dbConn.Open();
Trace.Assert(dbConn.State == ConnectionState.Open,"Error", "Connection failed...");
dr = dbComm.ExecuteReader(CommandBehavior.CloseConnection);
Trace.Assert(dr != null, "Error","The SqlDataReader is null!");
while (dr.Read()) {
}
} catch {
Trace.Fail("An error occurred in database access");
} finally {
if ((dr.IsClosed == false) && (dr != null))
dr.Close();
}
}
}