示例#1
0
        Boolean IsLogFilePastSizeLimit(IntPtr hFile, Int32 LimitBytes)
        {
            uint CurrentLen = 0;
            uint MoveMethod = 0;

            if (hFile == IntPtr.Zero)
            {
                return(false);
            }

            if (LimitBytes <= 0)
            {
                return(false);
            }

            CurrentLen = KernelImport.SetFilePointer(hFile, 0, out MoveMethod, KernelImport.EMoveMethod.End);

            if (CurrentLen == 0xFFFFFFFF)
            {
                return(false);
            }

            return(CurrentLen > LimitBytes);
        }
示例#2
0
        void AppendToOpenLogFile(IntPtr hFile, Boolean UseLogPrefix, params object[] Entries)
        {
            StringBuilder sb = null;

            byte[] buffer = null;
            long   Index;
            uint   uintval = 0;

            if (hFile == IntPtr.Zero)
            {
                return;
            }

            sb = new StringBuilder();

            sb.Append(DateTime.Now.ToString(LOGFILE_TIMEFMT) + LOGFILE_DELIMITER);

            if (UseLogPrefix)
            {
                sb.Append(LogMessagePrefix + LOGFILE_DELIMITER);
            }

            if (Entries.Length != 0 || Entries != null)
            {
                foreach (object val in Entries)
                {
                    if (!val.GetType().IsArray)
                    {
                        if (val is String)
                        {
                            sb.Append(val + LOGFILE_DELIMITER);
                        }
                        else
                        {
                            String converted_val;
                            try
                            {
                                converted_val = (String)Convert.ChangeType(val, typeof(String));
                                sb.Append(converted_val + LOGFILE_DELIMITER);
                            }
                            catch (Exception)
                            {
                                // ignore this value and move on ..
                            }
                        }
                    }
                }

                sb.Append(LOGFILE_LINEDELIMITER);

                // move to end of file
                Index = KernelImport.SetFilePointer(hFile, 0, out uintval, KernelImport.EMoveMethod.End);
                if (Index == 0xFFFFFFFF)
                {
                    return;
                }
                try
                {
                    buffer = Encoding.ASCII.GetBytes(sb.ToString());

                    // lock the required portion of file
                    if (KernelImport.LockFile(hFile, (uint)Index, 0, (uint)buffer.Length, 0) == false)
                    {
                        return;
                    }

                    // write and unlock
                    if (!KernelImport.WriteFile(hFile, buffer, (uint)buffer.Length, out uintval, IntPtr.Zero))
                    {
                        Index = KernelImport.GetLastError();
                    }

                    KernelImport.UnlockFile(hFile, (uint)Index, 0, (uint)buffer.Length, 0);
                    KernelImport.FlushFileBuffers(hFile);
                }
                catch (Exception) { }
            }
        }