Let's assume I have a C# file in my SP2010 application and I want to display a message in the browser console.

using System;
using System.Collections.Generic;

public class WhatEver{
    // some code goes here

    string str = "StackExchangeReader"

    // Console.log(str);  here the code I want to use to display

It could be in JavaScript or anything else as long as I have access to my variable. The purpose of this is to debug a production environnement application. No Visual Studio or any other solution that implies installing a third part application is allowed.

I've read this article which I found really interesting but the answer given does not work for me especially the MichaelTaylor3D's answer.

Thanks a lot.

  • 1
    You can log to a file or to sharepoint list. I think is more "natural". Aug 17 '16 at 13:12
  • you can use Page.RegisterClientScriptBlock, msdn.microsoft.com/en-us/library/… Aug 17 '16 at 13:14
  • 1
    Use the logging feature in SharePoint.. Use can log information into ULS, EventViewer etc. Aug 17 '16 at 13:54
  • ULS uses server stored logs and I have no access to them. Moreover, even if I could get the logs, what am I suppose to do with "the error was in the TwoThousandsLinesMethod" ? The logs aren't specific enough to be used efficiently. It would be great if I just could write my variable value somewhere it is quickly and easily accessible. @jpussacq gave the best answer until now.
    – ameliapond
    Aug 17 '16 at 14:16

I allways create a log in a SharePoint List. It is easy to filter and subscribe by mail. This is an example of C# to log in SPList:

public static void log(SPWeb web, string titulo, string detalle, string criticidad, string modulo, string sitioUrl, string sitioNombre)
    SPList listaLog = web.Site.RootWeb.Lists.TryGetList("Log");
    if (listaLog != null)
        web.Site.RootWeb.AllowUnsafeUpdates = true;
        SPListItem logItem = listaLog.Items.Add();
        SPFieldUrlValue sitio = new SPFieldUrlValue();
        sitio.Description = sitioNombre;
        sitio.Url = sitioUrl;
        logItem["Sitio"] = sitio;
        logItem["Título"] = titulo;
        logItem["Detalle"] = detalle;
        logItem["Criticidad"] = criticidad;
        logItem["Modulo"] = modulo;
        logItem["Fecha"] = DateTime.Now;
        web.Site.RootWeb.AllowUnsafeUpdates = false;

An alternate solution could be the use of the ULS (Unified Logging Service) by writing the variable content inside the log file whenever it is necessary.

using Microsoft.SharePoint.Administration;    
var i = "variableContent"
SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("My Category", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, i.toString(), "this parameter should normaly contain exception stack trace");

This snippet is normaly use inside a try/catch structure but I don't really need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.