【问题标题】:No logs are showing up in Linux CentOS 8 using .NET core 3 / 3.1使用 .NET core 3 / 3.1 的 Linux CentOS 8 中没有显示日志
【发布时间】:2020-06-13 17:45:28
【问题描述】:

我很想提供错误消息,但这就是我得到的全部......

我错过了什么吗? .NET core 3.1 与 2.1/2.2 中的 Linux 部署有什么额外变化吗?

我尝试尽快退出...

程序.cs

public class Program
    {
        public static void Main(string[] args)
        {
            bool dev = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";
            var logPath = dev ? "/Logs/gweb_log.txt" : "/var/log/gweb/gweb.log";

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                //.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                .Enrich.FromLogContext()
                //.WriteTo.AzureApp()
                .WriteTo.File(logPath, rollingInterval: RollingInterval.Day, fileSizeLimitBytes: 10485760, rollOnFileSizeLimit: true, retainedFileCountLimit: 3)
                .CreateLogger();

            try
            {
                Log.Information($"Garos_Web Started Utc: {DateTime.UtcNow}!");
                CreateHostBuilder(args).Build().Run();
            }

            catch (Exception e)
            {
                Log.Error($"Something went wrong. UTC: {DateTime.UtcNow}");
                Log.Error(e.Message);
                if (e.InnerException != null && !string.IsNullOrEmpty(e.InnerException.Message))
                {
                    Log.Error(e.InnerException.Message);
                }

                Log.Error(e.StackTrace);
            }
            finally
            {
                Log.CloseAndFlush();
            }

        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseSerilog()
                    .UseStartup<Startup>()
                    .UseKestrel(options =>
                    {
                        options.Listen(System.Net.IPAddress.Loopback, 5000);
                    });
                }
            );
    }

确保添加签名证书是新的,但我认为框架的一个很好的要求......

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
        {
            Configuration = configuration;
            WebHostEnvironment = webHostEnvironment;
        }

        public IConfiguration Configuration { get; }
        public IWebHostEnvironment WebHostEnvironment { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Configuration.GetSection("AppSettings").Bind(AppSettings.Default);

            services.AddDbContext<GarosWebDbContext>(options =>
                options.UseMySql(AppSettings.Default.ConnectionStrings.DefaultConnection));

            services.AddDefaultIdentity<GarosUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<GarosWebDbContext>();


            var certPath = Path.Combine(WebHostEnvironment.ContentRootPath, "garos_web_identity_server.pfx");
            var cert = new X509Certificate2(certPath, AppSettings.Default.IdentitySigningKey);

            services.AddIdentityServer()              
            .AddApiAuthorization<GarosUser, GarosWebDbContext>()
            .AddSigningCredential(cert);


            services.AddAuthentication()
                .AddIdentityServerJwt();

            services.AddControllersWithViews();

            services.AddRazorPages();

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            //app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseIdentityServer();
            app.UseAuthorization();


#if !DEBUG //Stop Registering users when in production (this is because we are using identity out of the box and don't want to allow more users at this time)
            app.UseAuthorization().Use((async (context, next) => {


                if (context.Request.RouteValues.Values.Contains("/Account/Register"))
                {
                    Debug.WriteLine("Inside");
                    context.Response.StatusCode = 404;                              
                }

                else
                {
                    await next();
                }
            }));
#endif
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
                endpoints.MapRazorPages();                
            });

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseReactDevelopmentServer(npmScript: "start");
                }
            });
        }
    }

服务器上的/etc/systemd/system/kestrel-webapplication.service文件

[Unit]
Description=GAROS_WEB .NET Web Application running on CentOS 8

[Service]
WorkingDirectory=/website_production/PublishOutput
ExecStart=/usr/share/dotnet/dotnet /website_production/PublishOutput/garos_web.dll
Restart=always
# restart service after 10 seconds if dotnet service crashes
RestartSec=10
SyslogIdentifier=garos_web_webapp
User=garos_web
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

【问题讨论】:

  • 您似乎正在向/var/log/gweb/gweb.log 写入日志,但正在读取/var/log/messages
  • 我试过了,但是当我检查时没有日志输出到/var/log/gweb/gweb.log。所以我希望就是这样。

标签: c# linux .net-core serilog centos8


【解决方案1】:

如何将.WriteTo.Console() 添加到该 serilog 设置中?您还需要在相关的 csproj 文件中包含此包:https://www.nuget.org/packages/serilog.sinks.console/

【讨论】:

  • 谢谢!终于不用拔头发就可以像正常人一样调试了!! [13:44:22 ERR] Something went wrong. UTC: 3/1/2020 12:44:22 PM [13:44:22 ERR] error:2006D080:BIO routines:BIO_new_file:no such file [13:44:22 ERR] at Interop.Crypto.CheckValidOpenSslHandle(SafeHandle handle) at Internal.Cryptography.Pal.OpenSslX509CertificateReader.FromFile(String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
猜你喜欢
  • 2020-07-07
  • 2020-12-26
  • 1970-01-01
  • 2020-09-26
  • 2020-11-20
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多