Code Snippets


Debug_Flag

public static bool Debug_Flag(
string sKey)
{
try
{
Microsoft.Win32.RegistryKey regKey;
object objObject;
bool bValue;

regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software");
regKey = regKey.OpenSubKey("VB and VBA Program Settings");
regKey = regKey.OpenSubKey("OfficeSolutions");
regKey = regKey.OpenSubKey(gsOFFICE_SOLUTION_GENERAL);

if (regKey == null)
{
// has not been installed correctly
return false;
}
else
{
objObject = regKey.GetValue(sKey);

if (objObject != null)
{
bValue = (bool)objObject;
return bValue;
}
else
{
return false;
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
return false;
}
catch (Exception ex)
{
gobjException = ex;
return false;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"DebugFlag",
"clsError",
"return the debug flag from the registry.",
gobjCOMException,
gobjException);
}
}
}

LogFile_Available

public string LogFile_Available(
clsSettingsFile objsettingsfile)
{
string sfilepath = "";

try
{
sfilepath = objsettingsfile.UserProfileFolder + objsettingsfile.LogFileName1;
if (modGeneral.File_AbleToWriteTo(sfilepath) == true)
{
objsettingsfile.LogFileNameAndExtension = objsettingsfile.LogFileName1;
return sfilepath;
}

sfilepath = objsettingsfile.UserProfileFolder + objsettingsfile.LogFileName2;
if (modGeneral.File_AbleToWriteTo(sfilepath) == true)
{
objsettingsfile.LogFileNameAndExtension = objsettingsfile.LogFileName2;
return sfilepath;
}

sfilepath = objsettingsfile.UserProfileFolder + objsettingsfile.LogFileName3;
if (modGeneral.File_AbleToWriteTo(sfilepath) == true)
{
objsettingsfile.LogFileNameAndExtension = objsettingsfile.LogFileName3;
return sfilepath;
}

modMessages.LogFile_NotAccessible(objsettingsfile);
return "";
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
return "";
}
}

LogFile_Purge

public void LogFile_Purge(
clsSettingsFile objsettingsfile)
{
string sfolderpath;
long lngFileSize;
int iLinesToDelete;
System.IO.FileInfo objFileInfo;
List objContentsList;

try
{
if (My.Settings.ERROR_OCCURRED == true)
return;

sfolderpath = LogFile_Available(objsettingsfile);

if (modGeneral.File_Exists(sfolderpath, true) == true)
{
objFileInfo = new System.IO.FileInfo(sfolderpath);
lngFileSize = objFileInfo.Length;

// if log file size is greater than 3MB
if (lngFileSize > 3146268)
{
objContentsList = System.IO.File.ReadAllLines(sfolderpath).ToList();

iLinesToDelete = (int)(objContentsList.Count / 2);
objContentsList.RemoveRange(0, iLinesToDelete - 1);

System.IO.File.WriteAllLines(sfolderpath, objContentsList.ToArray());
}
}
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
}
}

Tracer_Add

public void Tracer_Add(
string sCategory,
string sTracerMessage,
bool bAlwaysLogged = false,
System.Reflection.MethodBase objMethodBase = null)
{
string smessage = "";
string sdatetime = "";
string sSubroutineName = "";
string sDeclaringType = "";
string sClassName = "";

try
{
if (sCategory == "REGISTRY" ||
sCategory == "SYSTEM" ||
sCategory == "SERVICE" ||
sCategory == "PROGRESS" ||
sCategory == "EXCEPTION" ||
sCategory == "WARNING" ||
sCategory == "WORD" ||
sCategory == "MENU" ||
sCategory == "TOOLBAR" ||
sCategory == "COMMANDBAR" ||
sCategory == "FORM" ||
sCategory == "MESSAGE" ||
sCategory == "QUESTION")
{
bAlwaysLogged = true;
}

if (objMethodBase != null)
{
sSubroutineName = objMethodBase.Name;
sDeclaringType = objMethodBase.DeclaringType.ToString();
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1);
}

if (gSettingsFile != null)
{
if (gSettingsFile.DisplayDetailedLogging == false && bAlwaysLogged == false)
return;
}

sdatetime = DateTime_ToSensibleFormat(System.DateTime.Now, true);

if (sClassName.Length > 0)
{
smessage += "(" + sClassName + " - " + sSubroutineName + ") ";
}

smessage += sTracerMessage;

if (sCategory.Length > 0)
{
if (sCategory == "QUESTION" || sCategory == "MESSAGE")
System.Diagnostics.Trace.WriteLine("");

smessage = sCategory + ": " + smessage;
System.Diagnostics.Trace.WriteLine(smessage, sdatetime);

if (sCategory == "QUESTION" || sCategory == "MESSAGE")
System.Diagnostics.Trace.WriteLine("");
}
else
{
if (smessage.Length > 0)
System.Diagnostics.Trace.WriteLine(smessage);
else
System.Diagnostics.Trace.WriteLine("");
}
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex,
"",
false);
}
}

Tracer_AddService

public void Tracer_AddService(
string sServiceName,
object objServiceObject,
enSERVICES enOperation,
string sMethodName,
System.Reflection.MethodBase objMethodBase,
object objArgument = null)
{
string stypedescription = "";
string swebserviceaddress = "";
string sSubroutineName = "";
string sDeclaringType = "";
string sClassName = "";

DocviewUploadService2.DocviewUploadService2PortClient objDocviewUploadService2;
CFService.CFServicePortClient objCFService;

try
{
switch (enOperation)
{
case enSERVICES.INITIALISE:
stypedescription = "Initialise";
break;

case enSERVICES.REQUEST_START:
stypedescription = "Request Start";
break;

case enSERVICES.REQUEST_FINISH:
stypedescription = "Request Finish";
break;

case enSERVICES.COMPLETED_START:
stypedescription = "Completed Start";
break;

case enSERVICES.COMPLETED_FINISH:
stypedescription = "Completed Finish";
break;
}

if (objServiceObject == null)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
null,
"Unable to write the following line to the tracer because the 'objServiceObject' is set to Nothing" +
System.Environment.NewLine +
sServiceName + " - " + sMethodName + " (" + stypedescription + ")",
false);

return;
}

if (objMethodBase != null)
{
sSubroutineName = objMethodBase.Name;
sDeclaringType = objMethodBase.DeclaringType.ToString();
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1);
}

switch (sServiceName)
{
case "DocView":
objDocviewUploadService2 =
(DocviewUploadService2.DocviewUploadService2PortClient)objServiceObject;

swebserviceaddress = objDocviewUploadService2.Endpoint.Address.ToString();
break;

case "Credit":
objCFService =
(CFService.CFServicePortClient)objServiceObject;

swebserviceaddress = objCFService.Endpoint.Address.ToString();
break;
}

Tracer_Add2(
"SERVICE",
sServiceName + " - URL - " + swebserviceaddress + " - " + sMethodName + " (" + stypedescription + ")");

Tracer_Add2(
"SERVICE",
"(" + sClassName + " - " + sSubroutineName + ")");
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
}
}

Tracer_AddSubroutineFinish

public void Tracer_AddSubroutineFinish(
System.Reflection.MethodBase objMethodBase)
{
string sSubroutineName = "";
string sDeclaringType = "";
string sClassName = "";

try
{
if (objMethodBase != null)
{
sSubroutineName = objMethodBase.Name;
sDeclaringType = objMethodBase.DeclaringType.ToString();
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1);
}

Tracer_Add2(
"SUBROUTINE",
"(" + sClassName + " - " + sSubroutineName + ") finish");
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
}
}

Tracer_AddSubroutineStart

This can be added immediately after the "On Error GoTo ErrorHandler" to track exactly which subroutines and functions are called.
public void Tracer_AddSubroutineStart(
System.Reflection.MethodBase objMethodBase,
string sAdditionalText = "")
{
string sSubroutineName = "";
string sDeclaringType = "";
string sClassName = "";

try
{
if (objMethodBase != null)
{
sSubroutineName = objMethodBase.Name;
sDeclaringType = objMethodBase.DeclaringType.ToString();
sClassName = sDeclaringType.Substring(sDeclaringType.IndexOf(".") + 1);
}

Tracer_Add2(
"SUBROUTINE",
"(" + sClassName + " - " + sSubroutineName + ") start " + sAdditionalText);
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
}
}

Tracer_Finish

public void Tracer_Finish(
string sApplication)
{
string stracermessage = "";

try
{
Tracer_Add2("", "");

stracermessage += "----------------" +
sApplication.ToUpper() +
" SESSION ENDED: ";

stracermessage += modGeneral.DateTime_ToSensibleFormat(
System.DateTime.Now,
true,
true);

stracermessage += " ---------------";

System.Diagnostics.Trace.WriteLine(stracermessage);

Tracer_Add2("", "");
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
}
}

Tracer_Start

public void Tracer_Start(
string sApplication,
ref clsSettingsFile objSettingsFile)
{
string stracermessage = "";
string sfilepath;
System.IO.StreamWriter objStreamWriter;
System.Diagnostics.TextWriterTraceListener objTraceListener;

try
{
sfilepath = objSettingsFile.UserProfileFolder +
objSettingsFile.LogFileNameAndExtension;

objStreamWriter = System.IO.File.AppendText(sfilepath);
objTraceListener = new System.Diagnostics.TextWriterTraceListener(objStreamWriter);

System.Diagnostics.Trace.AutoFlush = true;
System.Diagnostics.Trace.Listeners.Add(objTraceListener);

Tracer_Add2("", "");

stracermessage += "----------------" +
sApplication.ToUpper() +
" SESSION STARTED: ";

stracermessage += modGeneral.DateTime_ToSensibleFormat(
System.DateTime.Now,
true,
true);

stracermessage += " ---------------";

System.Diagnostics.Trace.WriteLine(stracermessage);

System.Diagnostics.Trace.WriteLine(
"Add-in version: " +
gsVERSION_VSRELEASE +
" (" + My.Application.Info.Version.ToString() + ")");

System.Diagnostics.Trace.WriteLine(
"User Name: " +
System.Windows.Forms.SystemInformation.UserName);

System.Diagnostics.Trace.WriteLine(
"Region: " +
System.Globalization.RegionInfo.CurrentRegion.Name +
" (" + System.Globalization.RegionInfo.CurrentRegion.EnglishName + ")");

System.Diagnostics.Trace.WriteLine(
"Language: " +
System.Globalization.CultureInfo.CurrentCulture.EnglishName);

Tracer_Add2("", "");
}
catch (Exception ex)
{
modMessages.Exception(
System.Reflection.MethodBase.GetCurrentMethod(),
null,
ex);
}
}

© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top