示例#1
0
    protected static string CreateTABzip(DataTable[] myDT, string filename)
    {
        string info_msg = "";

        if (myDT == null)
        {
            info_msg += " **ERROR: DataTable array returned null.";
        }
        else
        {
            try
            {
                Object zip1 = new CreateZip(myDT, filename, "webserver", "zipdata");
                //Object zip2 = new CreateZip(myDT, filename, "browser");

                info_msg += "Created file:<br/><b>" + filename + ".zip</b><br/><br/>INFO Files:<br/>";
            }
            catch (Exception exc)
            {
                info_msg += "** ERROR creating .zip file **<br/>" + exc.Message;
            }
        }

        return(info_msg);
    }
        private void DefaultLogWriter(IList <LogItem> logItems)
        {
            try
            {
                //EventLogWriter.Log("Entering try block", EventLogEntryType.Information, 103);
                if (File.Exists(Location))
                {
                    var info = new FileInfo(Location);
                    //EventLogWriter.Log("File exists, file size is:" + info.Length, EventLogEntryType.Information, 104);
                    if (info.Length >= MaxFileSize)
                    {
                        //EventLogWriter.Log("File size is greater than max", EventLogEntryType.Information, 105);
                        //File.SetLastWriteTime(Location, DateTime.Now);
                        //var move = DateTime.Now;
                        ////EventLogWriter.Log("Attempting to move to: " + Location, EventLogEntryType.Information, 106);
                        //var loc = Location + "." + move.ToString("yyyy-dd-MM_HH-mm-ss_fffffff");
                        //info.MoveTo(loc);


                        var loc = Location + ".";
                        if (File.Exists(loc + "1"))
                        {
                            File.Delete(loc + "1");
                        }
                        info.MoveTo(loc + "1");

                        if (System.IO.File.Exists(loc + "1"))
                        {
                            int zipCount = 1;
                            while (System.IO.File.Exists(loc + zipCount + ".zip"))
                            {
                                zipCount++;
                            }

                            if (zipCount > MaxLogCount)
                            {
                                System.IO.File.Delete(loc + MaxLogCount + ".zip");
                                zipCount--;
                            }

                            if (zipCount > 1)
                            {
                                for (int i = zipCount; i > 1; i--)
                                {
                                    if (System.IO.File.Exists(loc + (i - 1) + ".zip") && !System.IO.File.Exists(loc + (i) + ".zip"))
                                    {
                                        System.IO.File.Move(loc + (i - 1) + ".zip", loc + (i) + ".zip");
                                    }
                                }
                            }

                            string fileName = Location.Substring(Location.LastIndexOf('\\') + 1);
                            CreateZip.CreateZipFile(Location + ".1", fileName);
                            System.IO.File.Delete(Location + ".1");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                EventLogWriter.Log(string.Format("Exception occurred in FileLoggerAppender -> DefaultLogWriter, {2}Message: {2}{0}{2}StackTrace: {2}{1}{2}Source: {2}{3}", e.Message, e.StackTrace, Environment.NewLine, e.Source), EventLogEntryType.Error, 4);
            }



            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(Location)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(Location));
                }
                using (var fs = new FileStream(Location, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 1024 * 1024, FileOptions.WriteThrough))
                {
                    using (var fw = new StreamWriter(fs, new UTF8Encoding(), 1024 * 1024, true))
                        // ReSharper disable ForCanBeConvertedToForeach Reason: Optimization
                        for (var i = 0; i < logItems.Count; i++)
                        // ReSharper restore ForCanBeConvertedToForeach
                        {
                            var toWrite = string.Format("{0}", Logger.FormatLog(DefaultLogPattern, logItems[i], _formatting));
                            fw.WriteLine(toWrite);
                        }
                    fs.Flush(true);
                }
            }
            catch (IOException e)
            {
                if (!IsFileLocked(e))
                {
                    EventLogWriter.Log(string.Format("IOException occurred in FileLoggerAppender -> DefaultLogWriter, {2}Message: {2}{0}{2}StackTrace: {2}{1}{2}Source: {2}{3}", e.Message, e.StackTrace, Environment.NewLine, e.Source), EventLogEntryType.Error, 3);
                    return;
                }
                Thread.Sleep(2000);
                DefaultLogWriter(logItems);
            }
            catch (ArgumentNullException e)
            {
                EventLogWriter.Log(string.Format("ArgumentNullException occurred in FileLoggerAppender -> DefaultLogWriter, {2}Message: {2}{0}{2}StackTrace: {2}{1}{2}Source: {2}{3}", e.Message, e.StackTrace, Environment.NewLine, e.Source), EventLogEntryType.Error, 1);
                // Sometimes a 'file not found' exception is thrown, not sure why
            }
            catch (Exception e)
            {
                EventLogWriter.Log(string.Format("Exception occurred in FileLoggerAppender -> DefaultLogWriter, {2}Message: {2}{0}{2}StackTrace: {2}{1}{2}Source: {2}{3}", e.Message, e.StackTrace, Environment.NewLine, e.Source), EventLogEntryType.Error, 2);
            }
        }