Sunday, July 28, 2019

Log4Net - Omitting the logs from the Root and appending to a different log file

I have set a root level logger which basically logs all.
There is a requirement to append a 3rd party information in a separate log file. Initially in the program, this would get logged even if you create a separate logger in the code, as the root captures all logging information.

        private static readonly ILog Logger
            = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private static readonly ILog ThirdPartyLogger = LogManager.GetLogger("ThirdPartyLogger");
...
...
...
ThirdPartyLogger.Info($"This gets logged in the main log too, even though the appender is not set in the root");

The trick to omit it from the root level logger is to have the "additivity" attribute set in the logger.

    <appender name="ServiceAppenderInfo" type="log4net.Appender.RollingFileAppender">
      <file value="C:\ProgramData\Service\ServiceLog_Info.log" />
      <encoding value="utf-8" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="20" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %message%n" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="INFO" />
      </filter>
    </appender>

    <appender name="ServiceAppenderError" type="log4net.Appender.RollingFileAppender">
      <file value="C:\ProgramData\Service\ServiceLog_Error.log" />
      <encoding value="utf-8" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %message%n" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="WARN" />
        <levelMax value="ERROR" />
      </filter>
    </appender>

    <appender name="ThirdPartyAppenderInfo" type="log4net.Appender.RollingFileAppender">
      <file value="C:\ProgramData\Service\ThirdParty_Info.log" />
      <encoding value="utf-8" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %message%n" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="INFO" />
      </filter>
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="ServiceAppenderInfo" />
      <appender-ref ref="ServiceAppenderError" />
    </root>
    <logger name="ServiceLogger">
      <level value="INFO" />
      <appender-ref ref="ServiceAppenderInfo" />
      <appender-ref ref="console"/>
    </logger>
    <logger name="ThirdPartyLogger" additivity="false">
      <level value="INFO" />
      <appender-ref ref="ThirdPartyAppenderInfo" />
    </logger>

Saturday, July 27, 2019

Entity Framework - Adding Auto Increment Column [Code First]

Use the attribute "DatabaseGenerated"

Example:

    public class Device
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int DeviceId { get; set; }
        public string Language { get; set; }
        public string RegistrationId { get; set; }
        public string DeviceType { get; set; }
        public int UserNo { get; set; }
    }

querying services with ARC (Advanced REST client)

Install ARC in Chrome


Add header details












Add body details