Exemplo n.º 1
0
            void write(Log.MessageType messageType, string message, string details = null)
            {
                lock (this)
                {
                    Writing?.Invoke(Name, messageType, message, details);

                    switch (Level)
                    {
                    case Level.NONE:
                        return;

                    case Level.ERROR:
                        if (messageType < MessageType.ERROR)
                        {
                            return;
                        }
                        break;

                    case Level.WARNING:
                        if (messageType < MessageType.WARNING)
                        {
                            return;
                        }
                        break;

                    case Level.INFORM:
                        if (messageType < MessageType.INFORM)
                        {
                            return;
                        }
                        break;

                    case Level.ALL:
                        break;

                    default:
                        throw new Exception("Unknown option: " + Level);
                    }

                    if (MaxFileSize > 0)
                    {
                        FileInfo fi = new FileInfo(File);
                        if (fi.Exists && fi.Length > MaxFileSize)
                        {
                            fileCounter++;
                            SetFile();
                        }
                    }

                    if (logWriter == null)
                    {
                        Directory.CreateDirectory(PathRoutines.GetFileDir(File));
                        logWriter = new StreamWriter(File, true);
                    }

                    message = (messageType == MessageType.LOG ? "" : messageType.ToString() + ": ") + message + (string.IsNullOrWhiteSpace(details) ? "" : "\r\n\r\n" + details);
                    logWriter.WriteLine(DateTime.Now.ToString(Log.TimePattern) + message);
                    logWriter.Flush();
                }
            }
        static Log()
        {
            {//this block works on Windows desktop, XamarinMAC, NT service, Android
                Assembly headAssembly = Assembly.GetEntryAssembly();
                //!!!when using WCF or Android, GetEntryAssembly() == NULL
                if (headAssembly == null)
                {
                    headAssembly = Assembly.GetCallingAssembly();
                }
                ProgramName = headAssembly.GetName(false).Name;

                //AppDir = AppDomain.CurrentDomain.BaseDirectory?.TrimEnd(Path.DirectorySeparatorChar);!!!gives not an app's dir on WCF or Android
                if (headAssembly.Location != null)
                {
                    AppDir = PathRoutines.GetFileDir(headAssembly.Location);
                }
                else//just in case. It hardly can come here
                {
                    Uri u = new Uri(headAssembly.CodeBase);
                    AppDir = PathRoutines.GetFileDir(u.LocalPath);
                }

                AssemblyRoutines.AssemblyInfo ai = new AssemblyRoutines.AssemblyInfo(headAssembly);
                CompanyName = ai.Company;
            }

            //{
            //    HashSet<Assembly> assemblies = new HashSet<Assembly>();
            //    Assembly a = null;
            //    StackTrace stackTrace = new StackTrace();
            //    foreach (StackFrame st in stackTrace.GetFrames())
            //    {
            //        Assembly b = st.GetMethod().DeclaringType.Assembly;
            //        if (b == null)
            //            break;
            //        a = b;
            //        assemblies.Add(a);
            //    }
            //    if (a == null)
            //        a = Assembly.GetEntryAssembly();

            //    AssemblyName = a.FullName;
            //    Process p = Process.GetCurrentProcess();
            //    AppDir = PathRoutines.GetFileDir(p.MainModule.FileName);
            //    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(p.MainModule.FileName);
            //    if (fvi != null)
            //    {
            //        CompanyName = fvi.CompanyName;
            //        //ProductName = fvi.ProductName;
            //    }
            //}

            //!!!No write permission on macOS
            //CompanyCommonDataDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + Path.DirectorySeparatorChar + CompanyName;
            //!!!No write permission on macOS
            //AppCompanyCommonDataDir = CompanyCommonDataDir + Path.DirectorySeparatorChar + ProcessName;
            //CompanyUserDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + CompanyName;
            //AppCompanyUserDataDir = CompanyUserDataDir + Path.DirectorySeparatorChar + ProcessName;
        }
 public static void MoveFile(string file1, string file2, bool overwrite = true)
 {
     CreateDirectory(PathRoutines.GetFileDir(file2), false);
     if (File.Exists(file2))
     {
         if (!overwrite)
         {
             throw new System.Exception("File " + file2 + " already exists.");
         }
         File.Delete(file2);
     }
     File.Move(file1, file2);
 }
Exemplo n.º 4
0
        void save()
        {
            __TypeVersion = __Info.TypeVersion;
            Saving();
            string s = Serialization.Json.Serialize(this, __Info.Indented, true, !__Info.NullSerialized, false /*!!!default values always must be stored*/);

            if (__Info.Endec != null)
            {
                s = __Info.Endec.Encrypt(s);
            }
            FileSystemRoutines.CreateDirectory(PathRoutines.GetFileDir(__Info.File));
            File.WriteAllText(__Info.File, s);
            Saved();
        }
Exemplo n.º 5
0
        static Log()
        {
            /*if (ProgramRoutines.IsWebContext) - !!!crashes on Xamarin!!!
             *  throw new Exception("Log is disabled in web context.");
             *
             * if (ProgramRoutines.IsWebContext)
             *  ProcessName = System.Web.Compilation.BuildManager.GetGlobalAsaxType().BaseType.Assembly.GetName(false).Name;
             * else*/
            /*
             * {//this block works on Windows desktop, XamarinMAC, NT service
             *  Assembly entryAssembly = Assembly.GetEntryAssembly();
             *  //!!!when using WCF, GetEntryAssembly() is NULL
             *  if (entryAssembly == null)
             *      entryAssembly = Assembly.GetCallingAssembly();
             *  ProcessName = entryAssembly.GetName(false).Name;
             *
             *  AssemblyRoutines.AssemblyInfo ai = new AssemblyRoutines.AssemblyInfo(entryAssembly);
             *  CompanyName = string.IsNullOrWhiteSpace(ai.Company) ? "CliverSoft" : ai.Company;
             *  ProductName = ai.Product;
             *
             *  AppDir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar);
             * }
             */
            {//this block works on Windows desktop, NT service.
                //!!!Needs testing on XamarinMAC
                Process p = Process.GetCurrentProcess();
                ProcessName = p.ProcessName;
                AppDir      = PathRoutines.GetFileDir(p.MainModule.FileName);
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(p.MainModule.FileName);
                if (fvi != null)
                {
                    CompanyName = fvi.CompanyName;
                    //ProductName = fvi.ProductName;
                }
            }

            //!!!No write permission on macOS
            //CompanyCommonDataDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + Path.DirectorySeparatorChar + CompanyName;
            //!!!No write permission on macOS
            //AppCompanyCommonDataDir = CompanyCommonDataDir + Path.DirectorySeparatorChar + ProcessName;
            //CompanyUserDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + CompanyName;
            //AppCompanyUserDataDir = CompanyUserDataDir + Path.DirectorySeparatorChar + ProcessName;
        }
Exemplo n.º 6
0
        public static IEnumerable <Process> GetProcesses(string exeFile)
        {
            string exeFileDir = PathRoutines.GetFileDir(exeFile).ToLower();

            return(Process.GetProcessesByName(PathRoutines.GetFileNameWithoutExtention(exeFile)).Where(p =>
            {
                ProcessModule pm;
                try
                {
                    pm = p.MainModule;
                }
                catch//sometimes it throws exception (if the process exited?)
                {
                    pm = null;
                }
                return pm == null ? false : pm.FileName.StartsWith(exeFileDir, StringComparison.InvariantCultureIgnoreCase);
            }
                                                                                                       ));
        }
 public static void Save(string file, object o, bool indented = true, bool polymorphic = true, bool ignoreNullValues = true, bool ignoreDefaultValues = false)
 {
     FileSystemRoutines.CreateDirectory(PathRoutines.GetFileDir(file));
     File.WriteAllText(file, Serialize(o, indented, polymorphic, ignoreNullValues, ignoreDefaultValues));
 }
 static public string GetAppDirectory()
 {
     return(PathRoutines.GetFileDir(GetAppPath()).TrimEnd('\\', '/'));
 }
 public static void CopyFile(string file1, string file2, bool overwrite = false)
 {
     CreateDirectory(PathRoutines.GetFileDir(file2), false);
     File.Copy(file1, file2, overwrite);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Creates the dir if it is missing.
 /// (!)It throws an exception when the destination file exists and !overwrite.
 /// </summary>
 /// <param name="file1"></param>
 /// <param name="file2"></param>
 /// <param name="overwrite"></param>
 public static void CopyFile(string file1, string file2, bool overwrite = false)
 {
     CreateDirectory(PathRoutines.GetFileDir(file2), false);
     File.Copy(file1, file2, overwrite);//(!)it throws an exception when the destination file exists and !overwrite
 }