20 using Microsoft.VisualBasic.Logging;
102 private static readonly
string _trigTargetEnvVar =
"AccuRevTriggers";
110 get {
return _trigTargetEnvVar; }
113 private static readonly
string _trigTargetFile =
"TriggersTarget.xml";
115 private static FileLogTraceListener _log;
116 [NonSerialized]
private static readonly
object _locker =
new object();
146 string localappdata = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
147 string paramFolder = Path.Combine(localappdata,
"AcTools\\Param");
148 if (!Directory.Exists(paramFolder))
149 Directory.CreateDirectory(paramFolder);
150 FileInfo fileInfoXml =
new FileInfo(xmlFile);
152 if (String.IsNullOrEmpty(command))
155 pFile = String.Format(
@"{0}\{1}-{2}-{3}.xml",
156 paramFolder, trigger, prncpl,
getStamp());
160 pFile = String.Format(
@"{0}\{1}-{2}-{3}-{4}.xml",
161 paramFolder, trigger, command, prncpl,
getStamp());
164 fileInfoXml.CopyTo(pFile,
true);
173 catch (Exception ecx)
175 Log($
"Exception caught and logged in AcDebug.paramFileCopy(string, ParamFileCopy, string, string, string){Environment.NewLine}{ecx.Message}");
215 string localappdata = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
216 string paramFolder = Path.Combine(localappdata,
"AcTools\\Param");
217 if (!Directory.Exists(paramFolder))
218 Directory.CreateDirectory(paramFolder);
219 string pFile = String.Format(
@"{0}\{1}-{2}-{3}.xml",
220 paramFolder, trigger, prncpl,
getStamp());
230 catch (Exception ecx)
232 Log($
"Exception caught and logged in AcDebug.paramFileCopy(ParamFileCopy, string, string, string){Environment.NewLine}{ecx.Message}");
250 FileStream fs = null;
253 if (File.Exists(fileName))
254 File.Delete(fileName);
256 fs =
new FileStream(fileName, FileMode.Create);
257 using (StreamWriter sw =
new StreamWriter(fs))
263 catch (Exception exc)
265 String msg = String.Format(
"Exception in AcDebug.createParamFile caught and logged.{0}{1}{0}Filename: {2}{0}{3}",
266 Environment.NewLine, exc.Message, fileName, content);
272 if (fs != null) fs.Dispose();
283 string stamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
284 DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() +
285 DateTime.Now.Millisecond.ToString();
336 string triggersFolder = Environment.GetEnvironmentVariable(_trigTargetEnvVar);
337 if (triggersFolder != null)
339 string xmlTriggersFile = Path.Combine(triggersFolder, _trigTargetFile);
340 XPathDocument doc =
new XPathDocument(xmlTriggersFile);
341 XPathNavigator nav = doc.CreateNavigator();
342 XPathNodeIterator iterDebug = nav.Select($
"Triggers/Trigger[@Name='{trigger}']/Principal");
343 foreach (XPathNavigator iterCmd
in iterDebug)
345 string name = iterCmd.GetAttribute(
"Name", String.Empty);
346 if (String.Equals(prncpl, name))
354 catch (XPathException ecx)
356 Log($
"XPathException in AcDebug.entryInTrigTargetsFile caught and logged.{Environment.NewLine}{ecx.Message}");
359 catch (Exception exc)
361 String msg = String.Format(
"Exception in AcDebug.entryInTrigTargetsFile caught and logged.{0}Trigger: {1}{0}Principal: {2}{0}{3}",
362 Environment.NewLine, trigger, prncpl, exc.Message);
378 public static void Log(
string message,
bool formatting =
true)
382 Console.WriteLine(message);
387 _log.WriteLine($
"{DateTime.Now.ToString()}{Environment.NewLine}{message}{Environment.NewLine}");
389 _log.WriteLine(message);
413 public static void Log(
string message,
string xmlParamFile,
bool formatting =
true)
415 Log(message, formatting);
420 using (StreamWriter writer =
new StreamWriter(xmlParamFile,
false))
422 writer.WriteLine(message);
428 Log($
"Unable to overwrite XML param file {xmlParamFile}{Environment.NewLine}{e.Message}");
478 string exeRootName = String.Empty;
479 using (Process currentProcess = Process.GetCurrentProcess())
481 ProcessModule pm = currentProcess.MainModule;
482 string module = pm.ModuleName;
483 exeRootName = Path.GetFileNameWithoutExtension(module);
486 string localappdata = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
487 string logsFolder = Path.Combine(localappdata,
"AcTools\\Logs");
488 if (!Directory.Exists(logsFolder))
489 Directory.CreateDirectory(logsFolder);
491 TraceSource ts =
new TraceSource(exeRootName);
492 _log = (FileLogTraceListener)ts.Listeners[
"AcLog"];
493 _log.Location = LogFileLocation.Custom;
494 _log.CustomLocation = logsFolder;
496 _log.BaseFileName = exeRootName;
500 _log.MaxFileSize = 83886080;
502 _log.ReserveDiskSpace = 2500000000;
504 _log.LogFileCreationSchedule = LogFileCreationScheduleOption.Weekly;
506 _log.AutoFlush =
true;
510 catch (Exception exc)
512 Console.WriteLine($
"Exception caught in AcDebug.initAcLogging{Environment.NewLine}{exc.Message}");
527 string logfile = null;
530 logfile = _log.FullLogFileName;
545 Exception e = (Exception)args.ExceptionObject;
556 if (!String.IsNullOrEmpty(errLine.Data))
558 string errline = errLine.Data.Trim();
560 if (errline.Length > 0 &&
561 !String.Equals(
"You are not in a directory associated with a workspace", errline))
static void Log(string message, string xmlParamFile, bool formatting=true)
Used by triggers, this method overload includes all functionality of Log(string, bool) and adds overw...
static bool paramFileCopy(ParamFileCopy pfcopy, string xml, string trigger, string prncpl)
Used by the server_auth_trig to save the content of the XML param data passed to it by AccuRev to %LO...
static void unhandledException(object sender, UnhandledExceptionEventArgs args)
Ensure that an unhandled exception gets logged to %LOCALAPPDATA%\AcTools\Logs\
-YYYY-MM-DD...
ParamFileCopy
For trigger development, debugging, and troubleshooting.
static bool initAcLogging()
Initialize application logging support, for general purpose or error messages, in conjunction with a ...
static void Log(string message, bool formatting=true)
Write the message text to STDOUT, to weekly log files located in %LOCALAPPDATA%\AcTools\Logs, and to trigger.log in the AccuRev server's ..storage\site_slice\logs folder in the case of triggers.
static void errorDataHandler(object sendingProcess, DataReceivedEventArgs errLine)
Centralized error handler.
static string getLogFile()
Get the log file's full path name.
static string TrigTargetEnv
Returns "AccuRevTriggers", the name of the environment variable on the AccuRev server that points to ...
static string getStamp()
Helper function for creating a unique filename for our copy of the XML param data.
static void createParamFile(string fileName, string content)
Helper function that creates the XML param data copy.
Use to log and display error and general purpose text messages, and to save the XML param data sent b...
static bool paramFileCopy(string xmlFile, ParamFileCopy pfcopy, string trigger, string prncpl, string command)
Used by a trigger to save the content of the XML param file passed to it by AccuRev to %LOCALAPPDATA%...
static bool entryInTrigTargetsFile(string trigger, string prncpl)
Determine if an entry exists for this user and trigger combo in TriggersTarget.xml.