private static void LogUnhandledException(Exception exception) { var errorDetails = exception?.ToString() ?? "(Unknown error)"; WinEventLog.Write(EventLogEntryType.Error, $@"Unhandled exception has occurred: {errorDetails}"); Log.Fatal( "*** Unhandled exception has occurred. The application will be terminated ***", exception); WriteConsole(); WriteConsole(ConsoleColor.Red, "*** UNHANDLED EXCEPTION ***"); WriteConsole(ConsoleColor.Red, errorDetails); WriteConsole(); }
private static void InitializeLogging(bool console) { WinEventLog eventLog; try { eventLog = new WinEventLog("CMSSync"); eventLog.LogInfo("Initializing CMSSync Service"); } catch (Exception e) { throw new ServiceStartupException("Service cannot be started because it cannot write to event log: {0} - {1}".Fmt(e.GetType().Name, e.Message)); } bool logVerbose = config.LogVerbose; #if DEBUG logVerbose = true; #endif if (!logVerbose && !console) { log = eventLog; } else { var clog = new CompositeLog(); if (console) { clog.AddLog(new ConsoleLog()); } if (logVerbose) { clog.AddLog(new FileLog(new FileInfo(Path.Combine(HomeFolder, "log.txt")))); } clog.AddLog(eventLog); log = clog; } log.LogDebug(""); log.LogDebug(DateTime.Now + " Starting service in folder " + HomeFolder); }
public static void downloadWithUniqueName(this Microsoft.Exchange.WebServices.Data.Attachment att, ref string path, int mtaNum) { try { Directory.CreateDirectory(path); path += ("\\" + EvaultWin.Common.evWinHash.MD5Hasing.GetMD5HashFromText(mtaNum.ToString() + EvaultWin.Common.evWinRandom.winRandomNumber.GetRandomName(DateTime.Now.ToString())) + Path.GetExtension(att.Name)); if (att is FileAttachment) { FileAttachment fa = att as FileAttachment; fa.Load(path); } else { ItemAttachment itemAtt = att as ItemAttachment; itemAtt.Load(new PropertySet(EmailMessageSchema.MimeContent)); MessageBox.Show("is item Att with path : " + path); //path += ".eml"; byte[] mc = itemAtt.Item.MimeContent.Content; using (FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write)) { fs.Write(mc, 0, mc.Length); } } } catch (Exception err) { path = string.Empty; WinEventLog.WriteEventLog("MTA failed to download attachment for scanning, error = " + err.Message, EventLogEntryType.FailureAudit); EvaultWin.DLP.evWinExceptionHandlingCMC.ExceptionHandling.DetailEventLog(true, false, EvaultWin.DLP.evWinExceptionHandlingCMC.EventExceptionType.UnexpectedResult, EvaultWin.DLP.evWinExceptionHandlingCMC.EventSeverityLevel.Error, err.Message, err.Source.ToString() + " : " + err.TargetSite.ToString(), err.ToString().Replace("\r", "").ToString()); //MessageBox.Show("Failed to download attachment, err : " + err.Message); } }
/// <summary> /// The main entry point for the application. /// </summary> public static int Main(string[] args) { ILog eventLog = new NullLog(); try { eventLog = new WinEventLog("CMSSync"); } catch (Exception e) { Console.Error.WriteLine("Could not write to event log - aborting. Details: {0} - {1}", e.GetType().Name, e.Message); return(-2); } if (args.Length > 0) { try { string firstArg = args[0].ToLowerInvariant(); string secondArg = null; if (args.Length > 1) { secondArg = args[1].ToLowerInvariant(); } switch (firstArg) { case "/d": case "-d": return(RunConsole()); //case "/init": // return RunConsole(true); case "/setcredential": return(SetCredentials(eventLog)); case "/update": return(UpdateCredentials(eventLog)); default: var myName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name; Console.Error.WriteLine("You must either run it as a service or specify the /setCredential argument to configure the credentials."); //Console.Error.WriteLine("USAGE: " + myName + "\n\t/setcredential\t\tconfigure the credentials.\n\t/update\t\tupdate credentials\n\t/d\t\tdebug mode"); Console.Error.WriteLine(@"To register it as a service: %Windir%\Microsoft.NET\Framework\v4.0.30319\installutil /LogFile=inst.log /LogToConsole=true " + myName + ".exe"); Console.Error.WriteLine(@"To unregister: %Windir%\Microsoft.NET\Framework\v4.0.30319\installutil /u " + myName + ".exe"); return(1); } } catch (Exception e) { ReportFailure("Unhandled exception: {0}".Fmt(e), eventLog); return(-1); } } else { // No command args - run as a service ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new AdPool() }; ServiceBase.Run(ServicesToRun); return(0); } }