Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bjarne Fyrstenborg 1072 posts 2803 karma points MVP c-trib
    5 days ago
    Bjarne Fyrstenborg
    0

    Write log to custom separate file

    When using the static LogHelper class in Umbraco it writes by default to UmbracoTraceLog file. https://our.umbraco.org/apidocs/csharp/api/Umbraco.Core.Logging.LogHelper.html

    Sometimes to might want to write to a separate file, e.g. when importing stuff. Is there a way to use LogHelper to write to a different file in some parts of the code or have a similar class which wrap to custom logger?

    At the moment I have the following in /Config/log4net.config

    <appender name="ProductImportAppender" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="App_Data\Logs\ProductImportLog.%property{log4net:HostName}.txt" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <maximumFileSize value="5MB" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value=" %date [P%property{processId}/D%property{appDomainId}/T%thread] %-5level %logger - %message%newline" />
        </layout>
        <encoding value="utf-8" />
      </appender>
    
      <logger name="ProductImportLogger" additivity="false">
        <level value="ERROR"/>
        <appender-ref ref="ProductImportAppender"/>
      </logger>
    

    Then in my import class I have.

    private static readonly ILog _logger = LogManager.GetLogger("ProductImportLogger");
    
    ...
    
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(_baseUrl);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        try
        {
            HttpResponseMessage response = await client.GetAsync("/GetProducts");
            if (response.IsSuccessStatusCode)
            {
                _logger.Info(string.Format("Successfully requesting product service. Returned Status Code: {0} {1}", (int)response.StatusCode, response.StatusCode));
    
                string jsondata = await response.Content.ReadAsStringAsync();
                return jsondata;
            }
            else
            {
                _logger.Error(string.Format("Error requesting product service. Returned Status Code: {0} {1}", (int)response.StatusCode, response.StatusCode));
            }
        }
        catch (HttpRequestException ex)
        {
            _logger.Error("Error requesting product service", ex);
        }
        //catch (TimeoutException ex)
        //{
        //    _logger.Error("Error request was timing out", ex);
        //}
        catch (TaskCanceledException ex)
        {
            _logger.Error("Error - request was cancelled", ex);
        }
    
        return "";
    }
    

    Can I somehow wrap the logger in a class similar to LogHelper so I don't need to call GetLogger() in the different files where I want to log details?

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft