/// <summary> /// 实现接口ILogger /// </summary> /// <typeparam name="TState"></typeparam> /// <param name="logLevel"></param> /// <param name="eventId"></param> /// <param name="state"></param> /// <param name="exception"></param> /// <param name="formatter"></param> public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { IsOpen = UtilConf.GetSection("WLogger")["IsOpen"] == "true" ? true : false; if (IsOpen) { //获取日志信息 var message = formatter?.Invoke(state, exception); //日志写入文件 LogToFile(logLevel, message); } }
private string DirectorySeparatorChar = Path.DirectorySeparatorChar.ToString();//目录分隔符 /// <summary> /// 获取文件名 /// </summary> private string GetFilename() { var dir = ""; WPath = UtilConf.GetSection("WLogger")["WPath"]; if (WPath.IndexOf(":") > -1) { dir = WPath; } else { //此方法不是真正的获取应用程序的当前方法,而是执行dotnet命令所在目录 dir = Directory.GetCurrentDirectory() + WPath; } if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var result = $"{dir}/WLog-{DateTime.Now.ToString("yyyy-MM-dd")}.txt".Replace("/", DirectorySeparatorChar); return(result); }