Exemplo n.º 1
0
        // Initialize Log4J
        public static string InitializeLog4NET()
        {
            // Setup log4j, currently the file is called log4net.xml
            string pafLog4NetFilename = Path.Combine(Application.StartupPath, @"App\Greenshot\" + LOG4NET_PORTABLE_FILE);
            string log4netFilename    = Path.Combine(Application.StartupPath, LOG4NET_FILE);

            if (File.Exists(log4netFilename))
            {
                try {
                    XmlConfigurator.Configure(new FileInfo(log4netFilename));
                    isLog4NetConfigured = true;
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, INIT_MESSAGE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else if (File.Exists(pafLog4NetFilename))
            {
                try {
                    XmlConfigurator.Configure(new FileInfo(pafLog4NetFilename));
                    isLog4NetConfigured = true;
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, INIT_MESSAGE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            if (!isLog4NetConfigured)
            {
                try {
                    Assembly assembly = typeof(LogHelper).Assembly;
                    using (Stream stream = assembly.GetManifestResourceStream("GreenshotPlugin.log4net-embedded.xml")) {
                        XmlConfigurator.Configure(stream);
                        isLog4NetConfigured = true;
                        IniConfig.ForceIniInStartupPath();
                    }
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, INIT_MESSAGE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            if (isLog4NetConfigured)
            {
                // Get the logfile name
                try {
                    if (((Hierarchy)LogManager.GetRepository()).Root.Appenders.Count > 0)
                    {
                        foreach (IAppender appender in ((Hierarchy)LogManager.GetRepository()).Root.Appenders)
                        {
                            if (appender is FileAppender)
                            {
                                return(((FileAppender)appender).File);
                            }
                        }
                    }
                } catch {}
            }
            return(null);
        }
Exemplo n.º 2
0
        // Initialize Log4J
        public static string InitializeLog4Net()
        {
            // Setup log4j, currently the file is called log4net.xml
            foreach (var logName in new[] { "log4net.xml", @"App\Greenshot\log4net-portable.xml" })
            {
                string log4NetFilename = Path.Combine(Application.StartupPath, logName);
                if (File.Exists(log4NetFilename))
                {
                    try
                    {
                        XmlConfigurator.Configure(new FileInfo(log4NetFilename));
                        _isLog4NetConfigured = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, InitMessage, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            // Fallback
            if (!_isLog4NetConfigured)
            {
                try {
                    Assembly assembly = typeof(LogHelper).Assembly;
                    using (Stream stream = assembly.GetManifestResourceStream("GreenshotPlugin.log4net-embedded.xml")) {
                        XmlConfigurator.Configure(stream);
                        _isLog4NetConfigured = true;
                        IniConfig.ForceIniInStartupPath();
                    }
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, InitMessage, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            if (_isLog4NetConfigured)
            {
                // Get the logfile name
                try {
                    if (((Hierarchy)LogManager.GetRepository()).Root.Appenders.Count > 0)
                    {
                        foreach (IAppender appender in ((Hierarchy)LogManager.GetRepository()).Root.Appenders)
                        {
                            var fileAppender = appender as FileAppender;
                            if (fileAppender != null)
                            {
                                return(fileAppender.File);
                            }
                        }
                    }
                }
                catch
                {
                    // ignored
                }
            }
            return(null);
        }