LayoutRenderer null

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

LayoutRenderer null

uranium60
Hi, guys.

I difined my custom LayoutRenderer.

[LayoutRenderer("user_id")]
    public class UserIdRenderer : LayoutRenderer
    {
        /// <summary>
        /// Dependency Injection
        /// </summary>
        public static IUserRepository Repository;

        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            User user = Repository.GetByUserName(HttpContext.Current.User.Identity.Name);
            builder.Append(user.Id);
        }
    }

I have Nlog.config where  I descibe interaction to the DataBase

<target name="EventLog" type="Database">
      <dbProvider>sqlserver</dbProvider>
      <connectionString>...</connectionString>
      <commandText>
        insert into Log_Events ([Event_Message],[User_ID]) values(@message, @user_id);
      </commandText>
      <parameter name="@message" layout="${message}" />
      <parameter name="@user_id" layout="${user_id}" />
  </target>

If the user id is null then there is no logging.

I looked in SQL profiler and saw

INSERT INTO LOG_EVENT ([EVENT_MESSAGE], [USER_ID] VALUES ("test","null")

How Do I fix?

"null" -> null

INSERT INTO LOG_EVENT ([EVENT_MESSAGE], [USER_ID] VALUES ("test",null)

Loading...