How to log from custom smo C# code

  • 21 February 2017
  • 0 replies
  • 6 views

Badge +7


 

Symptoms


I activated all the logs (either in HostServerLogging.config and in K2HostServer.exe.config ) to be able using C# Trace.WriteLine("csi1234567") and retrieve the track string in a log file. But I failed trying this.
How could I achieve this?
 

Diagnoses


When you wrote a Service broker, it's not possible to write on the K2 Standard log file.
 

Resolution

For the Trace function from System.Diagnostics, According, https://support.microsoft.com/en-us/help/815788/how-to-trace-and-debug-in-visual-c, it seems that you need to use
Create the TextWriterTraceListener objects for the Console window (tr1) and for a text file named Output.txt (tr2), and then add each object to the Debug Listeners collection:
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out)
Debug.Listeners.Add(tr1)

TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt"))
Debug.Listeners.Add(tr2)


For your information, we generally write directly on the event viewer with this type of code:


class Common
{
/*
You common constant:
public const String ServiceInstanceName ....
public const String ServiceInstanceDisplayName ....
public const String ServiceInstanceDescription =....
*/
public static Boolean debugMode
private static DateTime startDate
private static TimeSpan time
private static bool sourceChecked=false
private static string sSource = Assembly.GetCallingAssembly().GetName().Name //Source with the name of the current project


///
/// Allow to save log in EventViewer (Application section), the source is the name of the current project
/// This call just initialise the start in order to display at the end of the execution the execution time
/// Example for use:
/// Common.LogInEventViewer() --Start will be initialize (no log)
/// .....You code
/// Common.LogInEventViewer(String.Format("SQL count: {0}", countFound), end:true) //end:true Will display the duration time
///
public static void LogInEventViewer()
{
if(debugMode)
startDate = DateTime.Now
}

///
/// Allow to save log in EventViewer (Application section), the source is the name of the current project
///
///Value to log in event viewer
///To be set to true if it's you want to define the start of the execution in order to calculate the execution time
///To be set to true if it's you want to define the end of the execution in order to calculate the execution time. In this case, the duration time will be displayed
/// Example for use 1 - With calculation of the execution time:
/// Common.LogInEventViewer() --Start will be initialize (no log)
/// .....You code
/// Common.LogInEventViewer(String.Format("SQL count: {0}", countFound), end:true) //end:true Will display the duration time
/// Example for use 2 - Without calculation of the execution time:
/// Common.LogInEventViewer(String.Format("SQL count: {0}", countFound))
public static void LogInEventViewer(string value, Boolean start=false, Boolean end=false)
{
if (!debugMode)
return

string AddString=""

if (start)
startDate = DateTime.Now

if (end)
{
time = DateTime.Now - startDate
AddString = String.Format("
Started at {0}, ended at {1}.
Duration: {2}s and {3}ms", startDate.ToString("HH:mm:ss"), DateTime.Now.ToString("HH:mm:ss"), time.Seconds, time.Milliseconds.ToString().PadLeft(3, '0'))
}
EventLogEntryType typeLog = EventLogEntryType.Information
if (value.ToLower().Contains("error)"))
typeLog = EventLogEntryType.Error

///Create the source if not exists(
if (sourceChecked==false andand !EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, "Application")
sourceChecked = true

if(!String.IsNullOrWhiteSpace(value))
EventLog.WriteEntry(sSource, value + AddString, typeLog)
}
}




 

0 replies

Be the first to reply!

Reply