是否可以通过代码自定义天蓝色表存储? (Log4net.Azure) [英] Is it possible to customize azure table storage by code? (Log4net.Azure)
问题描述
所以我正在使用C#编写WebApi,我在我的web.config中配置了log4net(使用了log4net.azure),我的配置如下:
So I'm writing a WebApi using C#, I configured log4net in my web.config (used log4net.azure) my configuration is the following:
<appender name="AzureTableAppender" type="log4net.Appender.AzureTableAppender, log4net.Appender.Azure">
<param name="TableName" value="myLogs" />
<param name="ConnectionStringName" value="AzureTable" />
<param name="PropAsColumn" value="true" />
<bufferSize value="1" />
<param name="PartitionKeyType" value="LoggerName" />
</appender>
我的问题是,我得到了很多我不需要的额外列,现在我得到以下内容:
My issue is that I'm getting a lot of extra columns that I don't need, for now I'm getting the following:
我正在寻找一种方法,可以选择要显示的列,也许可以通过代码添加更多列,而无需使用我的azure帐户并取消我不想要的内容.
I'm searching for a way to be able to pick which columns to be displayed and maybe add more columns but by code without going to my azure account and untick what I don't want.
推荐答案
是的,您可以使用代码控制是否显示列.
Yes, you can control the columns to be displayed or not using code.
例如,假设您只希望在azure表存储中拥有4列:Message/Level/RoleInstance/DeploymentId,则可以按照以下步骤操作:
For example, assume that you wanna only have the 4 columns: Message / Level / RoleInstance / DeploymentId in your azure table storage, you can follow the steps:
第1步:创建一个包含上述4列的表格:
Step 1.Create a table contanis the above 4 columns:
public class AzureLog : TableEntity
{
public AzureLog()
{
var now = DateTime.UtcNow;
PartitionKey = string.Format("{0:yyyy-MM}", now);
RowKey = string.Format("{0:dd HH:mm:ss.fff}-{1}", now, Guid.NewGuid());
}
public string RoleInstance { get; set; }
public string DeploymentId { get; set; }
public string Message { get; set; }
public string Level { get; set; }
}
第2步:创建一个类,只需用于创建存储日志的表即可.在这里,我们将其命名为Logger:
Step 2.Create a class, just use to create the table where stored your log. Here we named it Logger:
public class Logger
{
//Storage credentials.
public StorageCredentials credentials = null;
//Storage account.
public CloudStorageAccount account = null;
//Table client
public CloudTableClient tableClient = null;
//Table.
public CloudTable table = null;
// Constructor.
public Logger(string tableName, string accountName, string accountKey)
{
//Create storage credentials object.
credentials = new StorageCredentials(accountName,
accountKey);
//Create storage account object.
account = new CloudStorageAccount(credentials, false);
//Create table client object.
tableClient = account.CreateCloudTableClient();
//Get the table reference.
table = tableClient.GetTableReference(tableName);
//Check whether table exist or not.
if (!table.Exists())
{
//Create the table if not exist.
table.Create();
}
}
// Insert into table.
public void Insert(AzureLog objAzureLog)
{
// Create the new insert table operation.
TableOperation insertOperation = TableOperation.Insert(objAzureLog);
// Execute the insert statement.
table.Execute(insertOperation);
}
}
第3步,定义您的自定义附加程序:
Step 3.define your custom appender:
public class AzureLogAppender : AppenderSkeleton
{
//Logger object.
private Logger objLogger = null;
//Azure table name.
public string tableName { get; set; }
//Azure account name.
public string accountName { get; set; }
//Azure account key.
public string accountKey { get; set; }
public override void ActivateOptions()
{
base.ActivateOptions();
//Logger object.
if (objLogger == null)
{
//Intilize logger object.
this.objLogger = new Logger(tableName, accountName, accountKey);
}
}
protected override void Append(LoggingEvent loggingEvent)
{
try
{
//Intilize AzureLog object.
AzureLog objAzureLog = new AzureLog
{
RoleInstance = "1",
DeploymentId = "100",
Message = loggingEvent.RenderedMessage,
Level = loggingEvent.Level.Name,
};
//Insert the log.
objLogger.Insert(objAzureLog);
}
catch (Exception ex)
{
//Handle exception here.
}
}
}
第4步,测试代码:
class Program
{
//Get the logger object.
private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
try
{
logger.Debug("Debug information.");
logger.Info("Info information.");
logger.Warn("Warn information.");
logger.Error("Error information.");
logger.Fatal("Fatal information.");
Console.WriteLine("ok");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}
第5步,我的app.config如下:
Step 5.My app.config as below:
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<appender name="AzureLogAppender" type="Log4NetAzureTables.AzureLogAppender, Log4NetAzureTables" >
<param name="tableName" value="your table name" />
<param name="accountName" value="your account" />
<param name="accountKey" value="your account key" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="AzureLogAppender" />
</root>
</log4net>
</configuration>
第6步:导航到表格,仅添加指定的列:
Step 6.Nav to table, only the specified columns are added:
这篇关于是否可以通过代码自定义天蓝色表存储? (Log4net.Azure)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!