Beispiel #1
0
 private void initKey()
 {
     try
     {
         /**
          * 获取秘钥
          * 没有秘钥则产生一对秘钥,并用非明文方式保存秘钥
          * */
         String strKey = FileReadWrite.Read();
         if (String.IsNullOrEmpty(strKey))
         {
             Key    = AesCryptoHelper.CreateKeyAndIv();
             strKey = SerializationHelper.Serialization(Key);
             strKey = AesCryptoHelper.Encrypt(strKey, keyEncryptorKey);
             FileReadWrite.Write(strKey);
         }
         else
         {
             strKey = AesCryptoHelper.Decrypt(strKey, keyEncryptorKey);
             Key    = SerializationHelper.Deserialization(strKey);
         }
     }
     catch (Exception ex)
     {
         var entity = LogEntityFactory.Create(String.Format("秘钥初始化失败:{0}", ex.ToString()),
                                              LogTypeFacotry.CreateExceptionLogType(),
                                              LogLevelFactory.CreateGravenessLogLevel());
         log.SaveLog(entity);
         throw new Exception("秘钥初始化失败,秘钥文件被破坏!");
     }
 }
Beispiel #2
0
        public void SimpleTest()
        {
            LogEntityFactory logEntityFactory = new LogEntityFactory();
            LogEntity        logEntity        = logEntityFactory.GetRandomLogEntity();
            ILogParser       logParser        = new CommonLogParser();
            DateTime         now = DateTime.UtcNow;

            try
            {
                var response = logParser.Parse(logEntity.ToString(), now);

                Assert.AreEqual(logEntity.IpAddress, response.IpAddress, "ip address");
                Assert.AreEqual(logEntity.UserId, response.UserId, "user id");
                Assert.AreEqual(logEntity.ClientIdentity, response.ClientIdentity, "client identity");
                Assert.AreEqual(logEntity.Time.ToShortTimeString(), response.Time.ToShortTimeString(), "time");
                Assert.AreEqual(logEntity.HttpMethod, response.HttpMethod, "http method");
                Assert.AreEqual(logEntity.HttpPath, response.HttpPath, "http path");
                Assert.AreEqual(logEntity.HttpVersion, response.HttpVersion, "http version");
                Assert.AreEqual(logEntity.StatusCode, response.StatusCode, "status code");
                Assert.AreEqual(logEntity.ResponseSize, response.ResponseSize, "size");
                Assert.AreEqual(now, response.TimeReadFromFile, "read from file");
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
Beispiel #3
0
 private void WriteLog(LogEntityFactory logEntityFactory)
 {
     foreach (var repository in Repositories)
     {
         repository.WriteLog(logEntityFactory);
     }
 }
        public override string WriteLog(LogEntityFactory logEntityFactory)
        {
            var logEntity = logEntityFactory.CreateLogEntity();

            MongoDbLogService.Insert(logEntity);

            return(logEntity.ToJSON());
        }
Beispiel #5
0
        public override string WriteLog(LogEntityFactory logEntityFactory)
        {
            var logEntity = logEntityFactory.CreateLogEntity();

            var line = logEntity.ToJSON();

            File.AppendAllText(FilePath, line);

            return(logEntity.ToJSON());
        }
Beispiel #6
0
        public override string WriteLog(LogEntityFactory logEntityFactory)
        {
            var logEntity = logEntityFactory.CreateLogEntity();

            this.SetConsoleForegroundColor(logEntity.Type);
            string line = logEntity.ToJSON();

            Console.WriteLine(line);

            return(logEntity.ToJSON());
        }
Beispiel #7
0
        static async Task WriteToFileAsync(CancellationToken ct)
        {
            try
            {
                var directoryPath    = Path.GetDirectoryName(LogSettings.FILE_PATH);
                var logEntityFactory = new LogEntityFactory();

                Log($"Checking if folder {directoryPath} exists...");
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                Log($"Using file {LogSettings.FILE_PATH} for logging...");

                using (Stream stream = File.Open(LogSettings.FILE_PATH, FileMode.Append, FileAccess.Write, FileShare.Read))
                    using (TextWriter writer = new StreamWriter(stream))
                    {
                        Stopwatch sw = Stopwatch.StartNew();

                        while (true)
                        {
                            // Give some feedback that we are doing something ... :)
                            if (sw.Elapsed > TimeSpan.FromSeconds(5))
                            {
                                Log("Still writing...");
                                sw.Restart();
                            }

                            var log = logEntityFactory.GetRandomLogEntity();
                            await writer.WriteLineAsync(log.ToString());

                            await writer.FlushAsync();

                            // Do we need to stop?
                            if (!ct.IsCancellationRequested)
                            {
                                await Task.Delay(_interval);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                Log($"Oops! Hit exception: {ex}");
            }
        }
Beispiel #8
0
        private void lvPlugin_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lvPlugin.SelectedItems.Count > 0)
            {
                try
                {
                    IPlugin plugin = this.lvPlugin.SelectedItems[0].Tag as IPlugin;
                    plugin.StartUp();
                }
                catch (Exception ex)
                {
                    var entity = LogEntityFactory.Create(String.Format("启动工具失败:{0}", ex.ToString()),
                                                         LogTypeFacotry.CreateExceptionLogType(),
                                                         LogLevelFactory.CreateGravenessLogLevel());
                    log.SaveLog(entity);

                    MessageBox.Show(ex.ToString(), "启动工具失败");
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// 加载插件
        /// </summary>
        private static void LoadPlugins()
        {
            Plugins = new List <IPlugin>();
            List <String> files = GetFiles(PluginsFolder);

            foreach (String filePath in files)
            {
                string fileName      = Path.GetFileName(filePath);
                string fileExtension = Path.GetExtension(filePath);

                if (fileName.Equals("MyDevTools.Plugin.dll", StringComparison.InvariantCultureIgnoreCase) ||
                    !_pluginExtensions.Contains(fileExtension))
                {
                    continue;
                }

                try
                {
                    Assembly pluginAssembly = Assembly.LoadFrom(filePath);
                    Type[]   exprotedTypes  = pluginAssembly.GetExportedTypes();

                    foreach (Type type in exprotedTypes)
                    {
                        if (!TypeIsIPlugin(type))
                        {
                            continue;
                        }

                        Plugins.Add((IPlugin)Activator.CreateInstance(type));
                    }
                }
                catch (Exception ex)
                {
                    var entity = LogEntityFactory.Create(String.Format("加载插件 {0} 时出现错误:{1}", fileName, ex.ToString()),
                                                         LogTypeFacotry.CreateExceptionLogType(),
                                                         LogLevelFactory.CreateWarningLogLevel());
                    log.SaveLog(entity);
                }
            }
        }
Beispiel #10
0
 public abstract string WriteLog(LogEntityFactory logEntityFactory);