Поскольку вы проверяете одну переменную на соответствие нескольким условиям, вам следует использовать что-то вроде этого. Здесь будет выполнен блок кода, где условие истинно, а другие блоки будут игнорироваться.
IF (@ParentVar1 Condition1)
Begin
IF(@Var1 Condition1)
BEGIN
/*Your Code Goes here*/
END
ELSE IF(@Var1 Condition2)
BEGIN
/*Your Code Goes here*/
END
ELSE IF(@Var1 Condition2)
BEGIN
/*Your Code Goes here*/
END
ELSE IF(@Var1 Condition2)
BEGIN
/*Your Code Goes here*/
END
ELSE --<--- Default Task if none of the above is true
BEGIN
/*Your Code Goes here*/
END
End
Просто напишите свой собственный метод « TraceLine (string msg)
» и начните его вызывать:
void TraceLine(string msg, bool OmitDate)
{
if (!OmitDate)
msg = DateTime.Now + " " + msg;
Trace.WriteLine(msg);
}
void TraceLine(string msg) {TraceLine(msg, false);}
You can configure the TraceOutputOptions flags enum.
var listener = new ConsoleTraceListener() { TraceOutputOptions = TraceOptions.Timestamp | TraceOptions.Callstack };
Trace.Listeners.Add(listener);
Trace.TraceInformation("hello world");
This does not work for Write and WriteLine, you have use the TraceXXX methods.
This can also be configured in your App.config with a somewhat equivalent and using TraceSource:
<configuration>
<system.diagnostics>
<trace autoflush="true">
<sources>
<source name="TraceSourceApp">
<listeners>
<add name="myListener" type="System.Diagnostics.ConsoleTraceListener" traceOutputOptions="Timestamp" />
</listeners>
</source>
</sources>
</trace>
</system.diagnostics>
</configuration>
And in code you can:
private static TraceSource mySource =
new TraceSource("TraceSourceApp");
static void Main(string[] args)
{
mySource.TraceInformation("hello world");
}
Вы можете написать метод-оболочку, который это сделает:
public static void DoTrace(string message)
{
DoTrace(message,true);
}
public static void DoTrace(string message, bool includeDate)
{
if (includeDate) {
Trace.WriteLine(DateTime.Now + ": " + message);
} else {
Trace.WriteLine(message);
}
}
Мы используем log4net TraceAppender, где вы можете легко настроить макет или фильтрацию.