In WorkerService File generation not working only for Linux .Net Core

旧时模样 提交于 2020-06-17 15:51:05

问题


I have to create a text file on linux when the service is starting. I tried to keep IsOSPlatform to recognize OS and based on it, I have written code to create a text file for logs. Below is the code which not generating the file only for linux, for windows it creates a file.

public static void Main(string[] args)
{
    bool isLinux = System.Runtime.InteropServices.RuntimeInformation
                                       .IsOSPlatform(OSPlatform.Linux);

    if(isLinux)
    {
        string sourceFile = System.IO.Path.Combine("~", "MyLog", "Config.xml");
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .Enrich.FromLogContext()
            .WriteTo.File(sourceFile).CreateLogger();
    }
    else
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .Enrich.FromLogContext()
            .WriteTo.File(@"C:\temp\Workerservice\logfile.txt").CreateLogger();
    }

    IHost host = CreateHostBuilder(args).Build();

    host.Services.UseScheduler(scheduler =>
    {
        scheduler
          .Schedule<ReprocessInvocable>()
          .EveryThirtySeconds();
    });
    host.Run();
}

How can I get it work?

来源:https://stackoverflow.com/questions/62255952/in-workerservice-file-generation-not-working-only-for-linux-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!