我最近刚刚在我们的软件中添加了一个AspNetCore项目,该项目不过是一个小型MVC站点。要托管此项目,我正在使用Microsoft.AspNetCore.Hosting
和Topshelf
的功能在Windows服务中运行托管。
问题在于,一旦在Windows服务中运行,我就无法从托管进程中获取任何调试信息。通常,所有信息都写入控制台,并且由于我在软件中使用自己的跟踪/日志记录,因此我希望尽可能地继续使用它,或者至少告诉托管过程将所有信息简单地转发给我们的跟踪方法调用不要错过任何东西,将来再实现像NLog这样的常见Logger。
这是托管的代码
public class Program
{
public static void Main(string[] args)
{
// Name of the executable
var nameOfExe = Process.GetCurrentProcess().MainModule.FileName;
// Path of the current executable
var pathToContentRoot = Path.GetDirectoryName(nameOfExe);
// Path of the www root for the static files
var pathToWebRoot = pathToContentRoot + @"\wwwroot";
IWebHost host = WebHost.CreateDefaultBuilder()
.UseKestrel()
.UseContentRoot(pathToContentRoot)
.UseIISIntegration()
.UseWebRoot(pathToWebRoot)
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.RunAsCustomService();
}
}
public static class WebHostServiceExtensions
{
public static void RunAsCustomService(this IWebHost host)
{
var webHostService = new Service(host);
ServiceBase.Run(webHostService);
}
}
public class Startup
{
public Startup(IHostingEnvironment env)
{
if(env.IsDevelopment())
{
env.ContentRootPath = env.ContentRootPath.Replace("Bin", @"Main");
env.ContentRootFileProvider = new PhysicalFileProvider(env.ContentRootPath);
env.WebRootPath = env.WebRootPath.Replace("Bin", @"Main");
env.WebRootFileProvider = new PhysicalFileProvider(env.WebRootPath);
}
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(500); });
// Add framework services.
services
.AddLocalization(options => options.ResourcesPath = "Resources")
.AddMvc().ConfigureApplicationPartManager(manager =>
{
var oldMetadataReferenceFeatureProvider = manager.FeatureProviders.First(f => f is MetadataReferenceFeatureProvider);
manager.FeatureProviders.Remove(oldMetadataReferenceFeatureProvider);
manager.FeatureProviders.Add(new ReferencesMetadataReferenceFeatureProvider());
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
services.AddSingleton<FFDModel>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.Configure<WebSettings>(Configuration.GetSection("ValidationFilters"));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSession();
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("de-DE"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=FFD}/{action=Index}/{id?}");
});
}
}
答案 0 :(得分:0)
我将使用Serilog
https://serilog.net登录任何.NET应用程序。
Serilog可以配置为将输出重定向到多个目标,例如控制台,滚动文件。如果您有自定义的日志记录目标/要求,则可以通过编写自定义Sink
来转发输出。
使用IWebHostBuilder
扩展名https://github.com/serilog/serilog-aspnetcore
来源