Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable file logging on IIS for serilog

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();
like image 544
CuriousGuy Avatar asked Dec 06 '25 22:12

CuriousGuy


2 Answers

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

like image 102
Mohammad Atiour Islam Avatar answered Dec 09 '25 18:12

Mohammad Atiour Islam


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

like image 32
menxin Avatar answered Dec 09 '25 20:12

menxin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!