Asp.Net Core 提供内置的日志,支持依赖注入,可以很方便地在控制器、Razor页面一级ViewComponent中使用。不过缺省提供的Provider只包括向控制台、Debug输出、Event Log等的输出,没有将日志记录到文件的缺省Provider,当然可以使用Log4Net等第三方日志插件,不过感觉太重了,最近发现了一个轻量级的日志插件,只需要一行代码的配置,就可以实现将日志输出到文件。首先,使用NuGet增加包 Serilog.Extensions.Logging.File到项目中,然后,在StartUp->Configure中增加如下设置
1 2 3 4 |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddFile("Logs/PlatCore-{Date}.txt"); } |
配置就完成了,由于实现了ILogger接口,因此使用方法与缺省的完全相同。在项目的Logs文件夹中,可以查看输出的日志文件。 作者:寻找无名的特质 链接:https://www.jianshu.com/p/fb7f064223f7 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View Details最近项目有个新同事,每个API接口里返回的时间格式中都带T如:【2019-06-06T10:59:51.1860128+08:00】,其实这个主要是ASP.Net Core自带时间格式列化时间格式设置的,我们只需要替换序格式化时间格式就可以; 一、先建一个控制器测试:
1 2 3 4 5 6 7 8 9 10 |
public IActionResult Get() { UserInfo userInfo = new UserInfo() { Name = "lxsh", BirthDay = DateTime.Now }; return Ok(userInfo); } |
二、没有替换格式化时间之前效果: 三、可以在注入mvc服务的时候设置格式化参数,在Startup类的ConfigureServices方法中指定
1 2 3 4 |
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options => { options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }); |
四、替换后的格式时间效果如下: from:https://www.cnblogs.com/lxshwyan/p/10983974.html
View Details【问题】网站 项目里经常用到DateTime类型变量,要求所有日期时间类型格式标准为“yyyy-MM-dd HH:mm:ss”,但默认Tostring格式为 yyyy-M-d H:m:s ,没有前置0, 需要修改 【分析】 1、修改代码里所有DateTime类型的Tostring为 ToString("yyyy-MM-dd HH:mm:ss"),不方便 2、找见统一的修改地方, Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN", true) { DateTimeFormat = { ShortDatePattern = "yyyy-MM-dd", FullDateTimePattern = "yyyy-MM-dd HH:mm:ss" }; 发现不起作用 经分析DateTimeFormat 类的属性,发现 LongTimePattern 起作用 【结论】 增加 LongTimePattern 属性
1 2 3 |
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN", true) { DateTimeFormat = { ShortDatePattern = "yyyy-MM-dd", FullDateTimePattern = "yyyy-MM-dd HH:mm:ss", LongTimePattern ="HH:mm:ss"} }; |
from:https://blog.csdn.net/onemetre/article/details/52042907
View Details1、从微软下载net core https://download.visualstudio.microsoft.com/download/pr/dd164132-d4c4-4c1a-8233-a4fc7e157935/bffa5312d613cab1a14f0858f947a6fc/dotnet-runtime-2.1.11-linux-x64.tar.gz 2、将安装包上传至linux中 3、解压 tar -vxf dotnet-runtime-2.1.11-linux-x64.tar.gz 4、创建链接,使所有目录下都可以访问到dotnet命令(这里是解压后放在/home/dotnetcore目录下) ln -s /home/dotnetcore/dotnet /usr/local/bin 5、测试验证是否成功 任意目录下:dotnet --info 最后,使用dotnet+dll名称,测试软件是否可以运行,如果无法运行,则安装相应的软件包即可 参考: 1、.NET Core 指南:https://docs.microsoft.com/zh-cn/dotnet/core/ 2、net core下载中心:https://dotnet.microsoft.com/download/dotnet-core from:https://blog.csdn.net/dqcoffee/article/details/91353543
View Details在部署的时候,如果您不想在您的Linux服务器上安装.Net Core SDK,您可以只安装Runtime,接下来我们看看该如何安装运行时Runtime。 下载运行时文件 下载页面:https://www.microsoft.com/net/download/linux 先获取一下对应的下载链接,可以使用浏览器点击链接来获取具体文件的下载链接 获取完链接以后,就可以使用命令下获取和安装了 以Centos 7,Ubuntu 16.04为例安装ASP.Net Core 2.0.5的运行时:
1 2 3 4 5 |
wget -O dotnet-runtime.tar.gz https://download.microsoft.com/download/1/1/0/11046135-4207-40D3-A795-13ECEA741B32/dotnet-runtime-2.0.5-linux-x64.tar.gz wget -O aspnetcore-store.tar.gz https://download.microsoft.com/download/1/1/0/11046135-4207-40D3-A795-13ECEA741B32/aspnetcore-store-2.0.5-linux-x64.tar.gz mkdir dotnet tar zxf dotnet-runtime.tar.gz -C dotnet tar zxf aspnetcore-store.tar.gz -C dotnet |
还需要安装 libunwind Centos 7
1 |
yum update -y && yum install libunwind libicu -y |
Ubuntu 16.04
1 |
apt-get update -y && apt-get install libunwind-dev -y |
原文地址:https://www.zkea.net/codesnippet/detail/post-85 from:https://www.cnblogs.com/seriawei/p/8438126.html
View Details最近在技术博客和技术交流群遇到很多小伙伴们在Linux下更新或者安装.Net Core SDK后dotnet命令无法识别等问题,现如下解决: 卸载SDK命令
1 |
sudo yum remove dotnet-sdk-* |
1 |
sudo yum remove libunwind libicu |
1 |
sudo yum remove /etc/yum.repos.d/dotnetdev.repo |
从新安装即可 from:https://www.cnblogs.com/yangzhili/p/9217083.html
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
2019-05-09 10:27:01,330 线程ID:[80] 日志级别:ERROR 出错类:WebApp.HttpGlobalExceptionFilter property:[(null)] - 错误描述:System.TypeInitializationException: The type initializer for 'System.DrawingCore.GDIPlus' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'gdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libgdiplus: cannot open shared object file: No such file or directory at System.DrawingCore.GDIPlus.GdiplusStartup(UInt64& token, GdiplusStartupInput& input, GdiplusStartupOutput& output) at System.DrawingCore.GDIPlus..cctor() --- End of inner exception stack trace --- at System.DrawingCore.GDIPlus.GdipGetGenericFontFamilySansSerif(IntPtr& fontFamily) at System.DrawingCore.FontFamily..ctor(GenericFontFamilies genericFamily) at System.DrawingCore.FontFamily.get_GenericSansSerif() at System.DrawingCore.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical) at Common.VerifyCodeHelper.CreateByteByImgVerifyCode(String verifyCode, Int32 width, Int32 height) in F:\src\WebApp\Common\VerifyCodeHelper.cs:line 206 at WebApp.Controllers.VerifyCodeController.NumberVerifyCode() in F:\src\WebApp\Controllers\VerifyCodeController.cs:line 27 at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync() |
解决方法 Centos 7 yum install libgdiplus-devel from:https://www.cnblogs.com/asd14828/p/10837140.html?utm_source=tuicool
View Details通过 Opw.HttpExceptions.AspNetCore 搞定了。 1)安装 nuget 包 Opw.HttpExceptions.AspNetCore
1 |
<PackageReference Include="Opw.HttpExceptions.AspNetCore" Version="2.3.0" /> |
2)Startup.ConfigureServices 中添加 AddHttpExceptions
1 |
services.AddControllers().AddHttpExceptions(); |
3)Startup.Configure 中添加 UseHttpExceptions
1 |
app.UseHttpExceptions(); |
示例响应输出
1 |
{"type":"error:invalid-cast","title":"InvalidCast","status":500,"detail":"Invalid cast from \u0027System.Int32\u0027 to \u0027Cnblogs.Infrastructure.Primitives.PostConfig\u0027.","instance":"/zzkdocuments"} |
from:https://q.cnblogs.com/q/121590/
View DetailsNET Core 应用程序相对于以前的.NET Framework 应用程序在启动运行的方式上有一定的差异,今天就来谈一谈这个获取应用程序启动路径的问题。 1.工作路径 WorkingDirectory 下面的两种方式都可以获取工作路径,结果都是一样的:
1 2 3 |
Environment.CurrentDirectory; Directory.GetCurrentDirectory(); |
其实所谓的工作路径就是我们应用程序的启动路径,所以我们平时所说的获取应用程序的启动路径,也是通过上面的方式。 (1)我们通过VS F5直接运行 VS会先编译我们的项目,输出到Debug\对应的sdk版本 目录下,然后以这个目录作为工作路径,启动我们的应用程序。 (2)通过dotnet 命令运行 我们在项目根目录,执行 dotnet run命令: 我们执行 dotnet run命令来启动时,对于程序的工作路径就是执行命令的路径,所以说,获取到的路径变化了。但是我们通过dotnet run命令运行的应用程序文件实际所在的目录也是和上面的目录一样的,即:Debug\对应的sdk版本,我们可以通过代码来测试一下: 新加的代码是获取程序集所在的路径,可以发现也是在 Debug\对应的sdk版本 目录下的。 我们将程序发布到 D:\test 目录下 可以看到,前两种方式获取到的都是执行dotnet命令所在的目录即工作目录,后一种方式是获取到的我们应用程序所在的目录。 2.结论 通过上面的测试,我们可以得出结论,.NET Core 应用程序获取工作路径/启动路径,就是获取的执行dotnet命令时所在的目录,所以当我们在Linux等系统部署时,设置守护进程时,记得一定要将工作路径设置为程序文件所在的目录,不然应用程序获取到的路径将不会是应用程序文件所在的目录,当我们在应用程序里设置了一些相对路径,诸如读取配置文件,写日志(Log4net、NLog),将会与我们的预期不一样。因为相对路径,是默认相对于应用程序的工作路径的。
1 2 3 4 5 |
Environment.CurrentDirectory; <span class="hljs-comment">//获取应用程序工作目录</span> Directory.GetCurrentDirectory();<span class="hljs-comment">//获取应用程序工作目录(和上面的方式效果是一样的)</span> Path.GetDirectoryName(<span class="hljs-keyword">typeof</span>(Program).Assembly.Location);<span class="hljs-comment">//获取应用程序所在目录(绝对,不受工作目录影响)</span> |
AppContext.BaseDirectory 也可以获取应用程序所在目录 from:https://www.cnblogs.com/stulzq/p/9220502.html
View Details第一步先注册session: 在Startup.cs文件中的ConfigureServices方法中添加:
1 |
services.AddSession(); |
在Startup.cs文件中的Configure方法中加上这一句代码
1 |
app.UseSession(); |
第二步从nuget安装Microsoft.AspNetCore.Mvc引用,直接使用自带的方法进行设置和获取session 不过自带的方法设置和获取的session值是byte[]类型的,可以从nuget安装并引用Microsoft.AspNetCore.Http并使用里面的扩展方法
1 |
using Microsoft.AspNetCore.Http; |
扩展后的方法为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/// <summary> /// 设置Session /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> protected void SetSession(string key, string value) { HttpContext.Session.SetString(key, value); } /// <summary> /// 获取Session /// </summary> /// <param name="key">键</param> /// <returns>返回对应的值</returns> protected string GetSession(string key) { var value = HttpContext.Session.GetString(key); if (string.IsNullOrEmpty(value)) value = string.Empty; return value; } |
下面这个图片里面的方法是自带的操作session的方法 下面这个图片是扩展的方法: from:https://www.cnblogs.com/dawenyang/p/9227713.html
View Details