根据文档,当应用程序关闭时,建议使用以下任一方式处理 Serilog
Log.CloseAndFlush();
Run Code Online (Sandbox Code Playgroud)
或者,如果您有一个实例变量 to .CreateLogger(),请调用它的
.Dispose();
Run Code Online (Sandbox Code Playgroud)
在 Visual Studio 中构建 ASP.NET Core 应用程序时,它会生成Program和Startup类,这两个类都没有实现IDisposable. 是不是要加上这个接口,Program才能直接配置Serilog?
我正在用NancyFx编写REST API.我经常得到这样的代码:
Post["/something"] = _ => {
// ... some code
if (success)
return HttpStatusCode.OK;
else
return someErrorObject;
};
Run Code Online (Sandbox Code Playgroud)
客户端始终假定application/json为所有响应的内容类型.它实际上是Accept: application/json在请求中设置的.application/json无论实际身体如何,没有错误的回答都是错误的.它只是检查内容类型,如果它与json不匹配则中止.我无法改变这种行为.
现在我面临的问题是,只需返回HttpStatusCode.OKNancy集,Content-Type: text/html但正如所说客户端假定application/json并且即使正文为空也会失败并出现错误.
我能够像这样强制内容类型:
return Negotiate
.WithContentType("application/json")
.WithStatusCode(HttpStatusCode.OK);
Run Code Online (Sandbox Code Playgroud)
我不想在任何地方重复这个代码.当然,我可以在一个函数中抽象它,但我正在寻找一个更优雅的解决方案.
有没有办法覆盖默认的Content-Type响应,以便return HttpStatusCode.OK将我的Content-Type设置为application/json?
我正在使用Froala 2,文档似乎没有任何内容暗示着一种简单的方式来设置插入标记的位置,更不用说在开始或结束时了。在某些情况下,我试图给编辑器实例添加少量内容,当我使用时html.set,插入号仅停留在开始处,而我想将其移至结尾。互联网似乎对v2没有任何帮助。
我正在从解决方案中删除对某些组件的依赖。其目录中的某些 C# 项目包含文件 licenses.licx,并在其文本 csproj 表示中引用了此文件。大多数 licenses.licx 仅引用我从解决方案中删除的组件。如何清理这个参考:
c# visual-studio licenses.licx emptylicenseslicx licenseslicx
尝试为 Serilog 2.8.0(在 .NET 4.6.2 中)设置最低日志级别。日志记录工作正常,但不是覆盖功能。
这是我的Program.cs:
using System;
using LogTest1.Classes;
using Microsoft.Extensions.Configuration;
using Serilog;
using SomeLib;
namespace LogTest
{
internal class Program
{
private static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Log.Verbose("Verbose");
Log.Debug("Debug");
Log.Information("Information");
Log.Warning("Warning");
Log.Error("Error");
Log.Fatal("Fatal");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
和appsettings.json文件:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Warning",
"Override": {
"LogTest": "Information"
}
},
"WriteTo": [
{ …Run Code Online (Sandbox Code Playgroud) 我最近恢复了丢失的文件,并且在我的C#项目中,“ licenses.licx”文件似乎已经消失了。
这是VS错误:
“找不到文件'C:... \ Exam \ Properties \ licenses.licx'”
如何解决此错误并重新生成“ licenses.licx”文件?
c# visual-studio licenses.licx emptylicenseslicx licenseslicx
我怎么能看到C#"语法糖"在幕后真正做了什么?例如,我知道C#lock语句已预编译为:
var temp = obj;
Monitor.Enter(temp);
try
{
// body
}
finally
{
Monitor.Exit(temp);
}
Run Code Online (Sandbox Code Playgroud)
我知道如果你直接在类中声明并初始化实例字段(而不是在构造函数中),那么它是用于在类中声明这些字段并在构造函数中初始化它们的语法糖.
我的问题是,如果我使用这些语法糖编写代码,我怎么能看到生成的C#代码是什么?
是否存在"预编译"过程,首先将这些语法糖转换为更复杂的C#代码,然后将其编译为CIL?
我正在使用滚动将所有日志写入一个文件。但我想通过Information,Warning和Exceptions滚动文件将它们分开。
我当前的配置是这样的
"Serilog": {
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}",
"pathFormat": "logs\\log-{Hour}.log",
"rollOnFileSizeLimit ": true,
"retainedFileCountLimit ": null,
"rollingInterval": "Hour",
"fileSizeLimitBytes": 5000000
}
}
]
},
Run Code Online (Sandbox Code Playgroud)
班级
public ILogger GetLogger()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
_logger =
new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
return _logger ;
}
Run Code Online (Sandbox Code Playgroud) c# ×6
serilog ×3
licenseslicx ×2
.net-core ×1
asp.net-core ×1
froala ×1
javascript ×1
log-level ×1
logging ×1
nancy ×1
overriding ×1