15

I want to enable file logging on IIS to resolve an issue locally on IIS. Following is the code I am using. It works when I run on visual studio and logs to a file but not when I deployed to IIS. Folder has enough permission to create a file and also I created the folder and file. What am I missing here ?

Program.cs

        var path = @"C:\workspace\Logs\Log-{Date}.txt";

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Verbose()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console(
                outputTemplate:
                "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}",
                theme: AnsiConsoleTheme.Literate)
            .WriteTo.File(path, fileSizeLimitBytes: 1_000_000,
                rollOnFileSizeLimit: true,
                shared: true,
                flushToDiskInterval: TimeSpan.FromSeconds(1))
            .CreateLogger();

enable useSerialLog()

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureServices(services => services.AddAutofac())
                .UseStartup<Startup>()
                .UseSerilog()
                .Build();
CuriousGuy
  • 3,818
  • 4
  • 24
  • 28
  • 1
    I can't spot any problems with the code - some kind of permissions issue would still be my best guess. Enabling `SelfLog` and sending its output somewhere else (a temporary text file?) may help uncover the cause: https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics – Nicholas Blumhardt Apr 19 '18 at 09:56

2 Answers2

12

Serilog works perfectly on local. For IIS the log file folder need IIS User Permission.

public Startup(IHostingEnvironment env)
        {
            Log.Logger = new LoggerConfiguration()
               .MinimumLevel
               .Information()
               .WriteTo.RollingFile(@"C:\inetpub\serilog\agilogoservicelogs\{Date}.txt", LogEventLevel.Information)
               .WriteTo.Seq("http://localhost:5341")
               .CreateLogger();

            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

Update your controller :

public class ValuesController : Controller
    {
        private readonly ILogger<ScrumUserController> _logger;

        public ValuesController(ILogger<ScrumUserController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<string> Get()
        {
            _logger.LogInformation("Agilibo Service start running");
            return new string[] { "Agilibo Core", "Service" };
        }
    }

Now give folder permission on deployed IIS.

enter image description here

Mohammad Atiour Islam
  • 5,380
  • 3
  • 43
  • 48
  • 1
    Setting the permissions to **Full control** for **IIS_IUSRS** and **DefaultAppPool** like suggested from the Serilog discussion here (https://github.com/serilog/serilog/issues/921) didn't help me either... Fun fact: The IIS **STDOUT** logging shows that the Serilog logging works theoretically... – FranzHuber23 Nov 28 '19 at 13:45
  • Okay, my error was way more stupid... **If debug** cause the issue... :D – FranzHuber23 Nov 28 '19 at 14:02
  • Hi I'm trying to create the file log inside the directory of my application published but the file never is created. When I start the application in Visual Studio all works but when I consume the application published the file is not created in that folder – kintela Dec 04 '19 at 11:24
  • @kintela please add folder permission for IIS_IUSRS, where the file will be write. – Mohammad Atiour Islam Jan 28 '20 at 17:16
6

If you use .net core ,because default application pool identity: ApplicationPoolIdentity has no write permission.

You should:

Change application pool user to Local System...

Or-

Give ApplicationPoolIdentity write permission:

ApplicationPoolIdentity is a virtual name , you can use "IIS AppPool\DefaultAppPool" set the permission.

enter image description here

you can use cli do the same work

ICACLS C:\sites\MyWebApp /grant "IIS AppPool\DefaultAppPool":F

Don't forget restart applicationpool and site.

From Microsoft Docs

menxin
  • 2,044
  • 1
  • 17
  • 15