我有ASP.NET Core web api项目.现在我想记录错误和事件.我之前在我的项目中使用过ELMAH,但似乎elmah不适用于ASP.NET Core.我提到了 这个官方链接, 用于配置Microsoft提供的默认日志记录服务.我看不到如何将这些日志保存在文本文件或数据库中.
如果ASP.NET Core已经具有默认日志记录功能,我想知道为什么我应该使用其他工具,如elmah,log4net.因此,当我搜索实现默认日志记录以将日志保存在db或文本文件中的文章时,我找不到任何内容.有没有什么方法可以使用ASP.NET核心内置的日志记录支持将日志保存到文件中?
我目前正在使用Serilog,它运行良好,并且还下载了seq,用于在浏览器中优雅地显示日志.但是,我仍然想知道如何使用内置的asp.net核心日志记录功能实现相同的功能.
logging serilog entity-framework-core asp.net-core asp.net-core-logging
dotnet restore在完成清除所有nuget缓存,dnx缓存和cli使用beta通道更新的过程后,我终于设法获得了一个项目的依赖项下载.
但使用project.json下面的内容,我得到一个错误;
警告:检测到的软件包降级:Microsoft.Dnx.Compilation.CSharp.Abstractions从1.0.0-rc2-16553到1.0.0-rc2-16552
我不确定这意味着什么,或者如何解决它.事情仍然下载,但这关心我.
我使用以下内容;
.NET Command Line Tools (1.0.0-beta-001540)
Product Information:
Version: 1.0.0-beta-001540
Commit Sha: 6aeed1f52d
Runtime Environment:
OS Name: Windows
OS Version: 10.0.10586
OS Platform: Windows
Runtime Id: win10-x64
Run Code Online (Sandbox Code Playgroud)
Active Version Runtime Architecture OperatingSystem Alias
------ ------- ------- ------------ --------------- -----
1.0.0-rc1-update1 clr x64 win
1.0.0-rc1-update1 clr x86 win
1.0.0-rc1-update1 coreclr x64 win
1.0.0-rc1-update1 coreclr x86 win
1.0.0-rc2-16551 clr x86 win
* 1.0.0-rc2-16551 coreclr x64 …Run Code Online (Sandbox Code Playgroud) 在ASP.NET Core 1.x中,我可以在Configure中使用身份验证方法,但现在在ASP.NET Core 2.0中,我必须在ConfigureServices中设置所有内容,并且无法在Configure方法中对其进行 配置.例如
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication()
.AddCookie()
.AddXX();
}
Run Code Online (Sandbox Code Playgroud)
然后在
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
....
app.UseAuthentication();
}
Run Code Online (Sandbox Code Playgroud)
在过去,我可以使用类似的东西
app.UseOpenIdConnectAuthentication();
Run Code Online (Sandbox Code Playgroud)
我不能再像这样配置它了.
那么我现在如何在ASP.NET Core 2.0中使用这样的东西呢?
app.Map(new PathString("/MyPath"), i => i.UseMyAuthMethod());
Run Code Online (Sandbox Code Playgroud) 我正在尝试在ASP.net 5中访问请求的原始输入正文/流.过去,我能够将输入流的位置重置为0并将其读入内存流但是当我尝试这样做时从上下文中,输入流为null或抛出错误(System.NotSupportedException =>"不支持指定的方法.").
在下面的第一个示例中,如果我将控制器方法的参数对象类型声明为动态,则可以在控制器中访问原始请求.由于各种原因,这不是一个解决方案,我需要在身份验证过滤器中访问原始请求正文.
此示例有效,但不是一个合理的解决方案:
[HttpPost("requestme")]
public string GetRequestBody([FromBody] dynamic body)
{
return body.ToString();
}
Run Code Online (Sandbox Code Playgroud)
引发错误:
[HttpPost("requestme")]
public string GetRequestBody()
{
var m = new MemoryStream();
Request.Body.CopyTo(m);
var contentLength = m.Length;
var b = System.Text.Encoding.UTF8.GetString(m.ToArray());
return b;
}
Run Code Online (Sandbox Code Playgroud)
引发错误:
[HttpPost("requestme")]
public string GetRequestBody()
{
Request.Body.Position = 0;
var input = new StreamReader(Request.Body).ReadToEnd();
return input;
}
Run Code Online (Sandbox Code Playgroud)
引发错误:
[HttpPost("requestme")]
public string GetRequestBody()
{
Request.Body.Position = 0;
var input = new MemoryStream();
Request.Body.CopyTo(input);
var inputString = System.Text.Encoding.UTF8.GetString(input.ToArray());
return inputString;
}
Run Code Online (Sandbox Code Playgroud)
我需要访问我正在构建的API的每个请求的原始请求主体.
任何帮助或方向将不胜感激!
编辑: …
我试图将我的ASP.NET核心路由的控制器限制到某个命名空间.
在以前版本的ASP.NET MVC中,有一个重载string[] namespaces在添加路由时提供了参数.ASP.NET MVC 6中缺少这个.所以在谷歌搜索之后,我尝试了类似的东西
app.UseMvc(routes => {
var dataTokens = new RouteValueDictionary {
{
"Namespaces", new[] {"ProjectA.SomeNamespace.Controllers"}
}
};
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}",
defaults: null,
constraints: null,
dataTokens: dataTokens
);
});
Run Code Online (Sandbox Code Playgroud)
但它似乎没有做我想要的.有没有办法将路由引擎限制到某个命名空间?
更新
我刚刚意识到它可能需要对我在每个控制器上使用属性路由这一事实做些什么?属性路由是否会影响到定义的路由app.UseMvc()?
更新2
更多细节:
我有两个完全独立的Web API项目.顺便提一下,一些路线在两者中都是相同的(即~/api/ping).这些项目在Production中是独立的,一个是用户的端点,一个是管理员的端点.
我也有单元测试,使用Microsoft.AspNet.TestHost.其中一些单元测试需要这两个Web API项目的功能(即需要"管理"端点来完全为"用户"设置测试用例).但是,当我引用两个API项目时,TestHost因为相同的路由而感到困惑,并抱怨"多个匹配的路由":
Microsoft.AspNet.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request
Microsoft.AspNet.Mvc.Infrastructure.AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
ProjectA.SomeNamespace.Controllers.PingController.Ping
ProjectB.SomeNamespace.Controllers.PingController.Ping
at …Run Code Online (Sandbox Code Playgroud) 在.Net中,我们必须Type.IsClass检查类型是否是使用的类System.Reflection.
但在.Net Core no.那么,我该怎么检查?
我为Visual Studio使用ASP.NET Core + Angular 2模板.我已经更新了我的project.json文件,它现在看起来(它的一部分):
"dependencies": {
"Microsoft.AspNetCore.AngularServices": "1.0.0-*",
"Microsoft.AspNetCore.Authentication.Cookies": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Diagnostics": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Hosting.Abstractions": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Identity": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Mvc": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview3-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-preview1-final",
"Microsoft.AspNetCore.StaticFiles": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.Design": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.Relational": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.Relational.Design": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.Sqlite": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0-preview1-final",
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview3-final",
"type": "build"
},
"Microsoft.EntityFrameworkCore.Tools.Core": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.CommandLine": "1.1.0-preview1-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0-preview1-final",
"Microsoft.Extensions.Configuration.Json": "1.1.0-preview1-final",
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.1.0-preview1-final",
"Microsoft.Extensions.FileProviders.Abstractions": "1.1.0-preview1-final",
"Microsoft.Extensions.Logging": "1.1.0-preview1-final",
"Microsoft.Extensions.Logging.Abstractions": "1.1.0-preview1-final",
"Microsoft.Extensions.Logging.Console": "1.1.0-preview1-final",
"Microsoft.Extensions.Logging.Debug": "1.1.0-preview1-final",
"Microsoft.Extensions.Options": "1.1.0-preview1-final",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0-preview1-final",
"Microsoft.Extensions.Primitives": …Run Code Online (Sandbox Code Playgroud) 我可以将数据发送到服务器,但仅当我使用FromBody-Attribute时.
为什么json数据不会使用Post自动从Body中读取?
后端web api
[HttpPost]
public async Task<IActionResult> Post([FromBody]CreateSchoolyearRequestDTO dto)
{
}
Run Code Online (Sandbox Code Playgroud)
前端angularjs
this.createSchoolyear = function (schoolyear) {
var path = "/api/schoolyears";
return $http({
url: path,
method: "POST",
data: schoolyear,
contentType: "application/json"
}).then(function (response) {
return response;
});
};
Run Code Online (Sandbox Code Playgroud) 使用Messenger课程的正确方法是什么?我知道它可以用于ViewModels/Views通信,但它是一个很好的方法用于技术/业务服务层吗?
例如,日志记录/导航服务在构造函数中注册某些消息,并且知道应用程序中何时出现这些消息.发件人(ViewModel ou Service)不引用服务接口,而只引用发送消息的信使.这是一个示例服务:
using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using App.Service.Interfaces;
using GalaSoft.MvvmLight.Messaging;
namespace App.Service
{
public class NavigationService : INavigationService
{
private PhoneApplicationFrame _mainFrame;
public event NavigatingCancelEventHandler Navigating;
public NavigationService()
{
Messenger.Default.Register<NotificationMessage<Uri>>(this, m => { this.NavigateTo(m.Content); });
}
public void NavigateTo(Uri pageUri)
{
if (EnsureMainFrame())
{
_mainFrame.Navigate(pageUri);
}
}
public void GoBack()
{
if (EnsureMainFrame()
&& _mainFrame.CanGoBack)
{
_mainFrame.GoBack();
}
}
private bool EnsureMainFrame()
{
if (_mainFrame != null)
{
return true;
}
_mainFrame …Run Code Online (Sandbox Code Playgroud) 使用 docker 和 asp.net core 进行开发时,我应该使用用户机密还是环境变量?我正在使用 Visual Studio 2017 在添加项目时创建的默认 docker 文件,该文件使用 microsoft/aspnetcore:1.1,我认为是 linux 映像。
如何在 docker 中设置用户机密/环境变量,以便在启动时设置它们,但不包含在源代码中?
asp.net-core ×7
.net-core ×4
c# ×2
angularjs ×1
asp.net ×1
docker ×1
http-post ×1
logging ×1
messaging ×1
mvvm ×1
mvvm-light ×1
reflection ×1
serilog ×1