public void Logger_Log_DelegatesToNLog() { var target = GetLogMemoryTarget(); ILogger log = new NLogLogger(LogManager.GetLogger("cat")); log.Info("msg"); Assert.Equal("cat|Info|msg||", target.Logs.Last()); log.Log(LogLevel.Info, "msg"); Assert.Equal("cat|Info|msg||", target.Logs.Last()); log.Debug("msg"); Assert.Equal("cat|Debug|msg||", target.Logs.Last()); log.Log(LogLevel.Debug, "msg"); Assert.Equal("cat|Debug|msg||", target.Logs.Last()); log.Warn("msg"); Assert.Equal("cat|Warn|msg||", target.Logs.Last()); log.Log(LogLevel.Warning, "msg"); Assert.Equal("cat|Warn|msg||", target.Logs.Last()); log.Error("msg", new Exception("ex")); Assert.Equal("cat|Error|msg|ex|", target.Logs.Last()); log.Log(LogLevel.Error, "msg", new Exception("ex")); Assert.Equal("cat|Error|msg|ex|", target.Logs.Last()); log.Fatal("msg"); Assert.Equal("cat|Fatal|msg||", target.Logs.Last()); log.Fatal("msg", new Exception("ex")); Assert.Equal("cat|Fatal|msg|ex|", target.Logs.Last()); log.Log(LogLevel.Fatal, "msg", new Exception("ex")); Assert.Equal("cat|Fatal|msg|ex|", target.Logs.Last()); }
protected override void OnIncomingError(ExceptionContext exceptionContext, IHubIncomingInvokerContext invokerContext) { var logger = new NLogLogger("SignalR"); logger.Log(Logger.LoggerLevels.Error, "=> Exception " + exceptionContext.Error.Message); logger.Log(Logger.LoggerLevels.Error, exceptionContext.Error.ToString()); if (exceptionContext.Error.InnerException != null) { logger.Log(Logger.LoggerLevels.Error, "=> Inner Exception " + exceptionContext.Error.InnerException.Message); } base.OnIncomingError(exceptionContext, invokerContext); }
/// <summary> /// Provides an NLog-Loggers to config using NLog.config file. /// </summary> /// <returns>The NLogLogger.</returns> protected override ILoggerFacade CreateLogger() { ILoggerFacade logger = new NLogLogger(); logger.Log("${SolutionName} Logger was created.", Category.Info, Priority.None); return logger; }
protected T ExecuteWithExceptionHandledOperation <T>(Func <T> func, string errorText = null) where T : class { try { var result = func.Invoke(); return(result); } catch (Exception ex) { ex.Data.Add("errorText-business", errorText); NLogLogger.Log(ex, "APPLICATION", LogPriorityEnum.High); if (string.IsNullOrEmpty(errorText)) { errorText = "Yapılan işlem sırasında hata oluştu."; } System.Type type = typeof(T); //aynı tip oluşturulur. ConstructorInfo magicConstructor = type.GetConstructor(System.Type.EmptyTypes); //constructure oluşturulur. object magicClassObject = magicConstructor.Invoke(new object[] { }); //sınıf oluşturulur. MethodInfo methodInfo = type.GetMethod("Fail"); //oluşturulan sınıfın Fail metodu bulunur. methodInfo.Invoke(magicClassObject, new object[] { 500, ex.HelpLink == "CustomException" ? ex.Message : errorText }); //ilgili metodu gelen parametrelerle çağırırız. return(magicClassObject as T); } }
public Account GetAccountByUserName(string UserName) { Account entity = new Account(); try { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); System.Text.RegularExpressions.Match match = regex.Match(UserName); //if (UserName.Contains("@") && UserName.Contains(".")) if (match.Success) // username mail adresi ise mail ile useri getir { entity = _context.Account .Include(r => r.AccountType) .SingleOrDefault(r => r.IsActive && r.Email == UserName.Trim()); } else { entity = _context.Account .Include(r => r.AccountType) .SingleOrDefault(r => r.IsActive && r.UserName == UserName.Trim()); } } catch (Exception ex) { entity = null; NLogLogger.Log(ex, "INFRASTRUCTURE_Repo", LogPriorityEnum.High); } return(entity); }
/// <summary> /// Provides an NLog-Loggers to config using NLog.config file. /// </summary> /// <returns>The NLogLogger.</returns> protected override ILoggerFacade CreateLogger() { ILoggerFacade logger = new NLogLogger(); logger.Log("${SolutionName} Logger was created.", Category.Info, Priority.None); return(logger); }
protected void ExecuteWithExceptionHandledOperation(Action action) { try { action.Invoke(); } catch (Exception ex) { NLogLogger.Log(ex, "BUSINESS", LogPriorityEnum.High); } }
public AccountType GetTypeByCode(string TypeCode) { AccountType entity = new AccountType(); try { entity = _context.AccountType .SingleOrDefault(r => r.IsActive && r.TypeCode == TypeCode.Trim()); } catch (Exception ex) { entity = null; NLogLogger.Log(ex, "INFRASTRUCTURE_Repo", LogPriorityEnum.High); } return(entity); }
/* * public List<View_GetAccountMovie_Result> AccountMovieList(int? AccountId = 0, int? MovieId = 0) * { * List<View_GetAccountMovie_Result> entities = new List<View_GetAccountMovie_Result>(); * * try * { * * var conn = _context.Database.GetDbConnection(); * var command = conn.CreateCommand(); * conn.Open(); * string _query = "Select * from view_getaccountmovie where 1=1 "; * if (AccountId > 0) * _query += ("and AccountId = " + AccountId); * if (MovieId > 0) * _query += ("and MovieId = " + MovieId); * command.CommandText = _query; * var reader = command.ExecuteReader(); * * while (reader.Read()) * { * var record = new View_GetAccountMovie_Result * { * AccountMovieId = (int)reader["AccountMovieId"], * AccountId = (int)reader["AccountId"], * UserFullName = (string)reader["UserFullName"], * TeamId = (int)reader["MovieId"], * Title = (string)reader["Title"], * IsActive = (bool)reader["IsActive"] * }; * * entities.Add(record); * } * * //conn.Dispose(); * conn.Close(); * } * catch (Exception ex) * { * entities = null; * NLogLogger.Log(ex, "INFRASTRUCTURE_Repo", LogPriorityEnum.High); * } * * * return entities; * } */ public AccountMovie GetAccountMovieCoupling(int AccountId, int MovieId) { AccountMovie entity = new AccountMovie(); try { entity = _context.AccountMovie .SingleOrDefault(r => r.IsActive && r.Id == MovieId && r.AccountId == AccountId); } catch (Exception ex) { entity = null; NLogLogger.Log(ex, "INFRASTRUCTURE_Repo", LogPriorityEnum.High); } return(entity); }
public static void Register() { ServicesConfig.GetSmsServiceByTenantId = new Func <object, ISmsService>((tenantKey) => { using (var db = new AppDbContext()) { var logger = new NLogLogger("Sms"); var tenantId = (int)tenantKey; var config = db.Settings_AliMsgs.FirstOrDefault(p => p.TenantId == tenantId); if (config != null) { return(new AlidayuSmsService(logger, config.AppKey, config.Secret)); } logger.Log(Logger.LoggerLevels.Error, "您尚未配置短信接口,将无法发送短信"); return(null); } }); }
public List <AccountMovie> AccountMovieList(int?AccountId = 0, int?MovieId = 0) { List <AccountMovie> entities = new List <AccountMovie>(); try { entities = _context.AccountMovie .Where(r => r.IsActive && (!MovieId.HasValue || r.Id == MovieId) && (!AccountId.HasValue || r.AccountId == AccountId)).ToList(); } catch (Exception ex) { entities = null; NLogLogger.Log(ex, "INFRASTRUCTURE_Repo", LogPriorityEnum.High); } return(entities); }
public void Configuration(IAppBuilder app) { GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection"); app.UseHangfireServer(); app.UseHangfireDashboard(); var logger = new NLogLogger("Startup"); ConfigureAuth(app); //判断是否启用SignalR if ((ConfigurationManager.AppSettings["EnableSignalR"] != null) && (ConfigurationManager.AppSettings["EnableSignalR"].ToLower() == "true")) { GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule()); app.MapSignalR(); } logger.Log(Logger.LoggerLevels.Debug, "OwinStartup 启动完成"); }
public static void Builder() { NotifyBuilder <Site_Notify> .Create() .WithOnReconnected((context, groups) => { var logger = new NLogLogger("Notify"); logger.Log(Logger.LoggerLevels.Debug, "OnReconnected..."); }) .WithOnConnected((context, groups) => { var logger = new NLogLogger("Notify"); logger.Log(Logger.LoggerLevels.Debug, "OnConnected..."); if (context.User.Identity.IsAuthenticated) { var userId = context.User.Identity.GetUserId(); using (AppDbContext db = new AppDbContext()) { var userKey = "User_" + userId; groups.Add(context.ConnectionId, userKey); var user = db.Users.Find(userId); //根据登录角色获取 var tenantId = user.TenantId; var agentTennantId = user.AgentTennantId; //如果当前租户为系统租户,则允许代理其他租户操作 if (db.Admin_Tenants.Any(p => (p.Id == tenantId) && p.IsSystemTenant)) { groups.Add(context.ConnectionId, "Admin"); //系统租户使用AgentTennantId if ((user.AgentTennantId != default(int)) && (user.AgentTennantId != tenantId)) { tenantId = user.AgentTennantId; } } var tenantKey = "Tenant_" + tenantId; groups.Add(context.ConnectionId, tenantKey); //连接时通知 var notifier = WeiChatApplicationContext.Current.Notifier; var queryable = db.Site_Notifies.Where(p => (p.Receiver == userKey || p.Receiver == tenantKey) && !db.Site_ReadNotifies.Any(p1 => p1.NotifyId == p.Id && p1.CreateBy == userId)); var lastNotifies = queryable.OrderByDescending(p => p.UpdateTime).Take(8).ToList <INotifyInfo>(); notifier.NotifyTo(lastNotifies, userKey); } } }) .WithOnClientNotify((notifyStr, groupName, context, groups) => { if (context.User.Identity.IsAuthenticated) { var userId = context.User.Identity.GetUserId(); var notifyInfo = JsonConvert.DeserializeObject <Site_Notify>(notifyStr); using (var db = new AppDbContext()) { if (notifyInfo.Id == default(int)) { notifyInfo.CreateTime = DateTime.Now; notifyInfo.CreateBy = userId; notifyInfo.UpdateTime = DateTime.Now; notifyInfo.Receiver = groupName; db.Site_Notifies.Add(notifyInfo); } else { notifyInfo.UpdateTime = DateTime.Now; notifyInfo.Href = notifyInfo.Href; notifyInfo.IconCls = notifyInfo.IconCls; notifyInfo.IsTaskFinish = notifyInfo.IsTaskFinish; notifyInfo.Message = notifyInfo.Message; notifyInfo.Receiver = groupName; notifyInfo.TaskPercentage = notifyInfo.TaskPercentage; notifyInfo.Title = notifyInfo.Title; db.Entry(notifyInfo).State = EntityState.Modified; } db.SaveChanges(); return(notifyInfo); } } return(null); }) .Build(); WeiChatApplicationContext.Current.Notifier = new SignalRNotifier <Site_Notify>(); }